目录

FatMouse's Speed

解题思路:

ac代码:


FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21117    Accepted Submission(s): 9367
Special Judge

Problem Description

FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.

Input

Input contains data for a bunch of mice, one mouse per line, terminated by end of file.

The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.

Two mice may have the same weight, the same speed, or even the same weight and speed.

Output

Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],..., m[n] then it must be the case that

W[m[1]] < W[m[2]] < ... < W[m[n]]

and

S[m[1]] > S[m[2]] > ... > S[m[n]]

In order for the answer to be correct, n should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.

Sample Input

6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900

Sample Output

4
4
5
9
7

解题思路:


结构体内先以weight为基准排序,再排speed,排完序后weight可能有相等的情况 ,再找符合条件的speed的最长递减子序列

(最长递减子序列求解的方法参见https://blog.csdn.net/Cassie_zkq/article/details/82900389,思路相同)

如何解决输出的序号的问题呢?

1)last记录最长递减子序列最后一个元素的排序后的序号;

2)pre[i]数组记录dp[i]对应的子序列的倒数第二个元素,最后就可以找到dp[m](假设dp[m]是最大的)中所以元素的排完序的序号了;

3)out[]数组记录输出;

举个栗子:

输出4个元素,这4个元素在sort之后的顺序依次是 4 ,5,7,9

此时last=9,

且已经记录 :pre[9]=7, pre[7]=5 , pre[5]=4, pre[4]=0;

则输出为out[0]=9,out[1]=7,out[2]=5,out[3]=4;(用while即可实现,详见代码)

最后再倒序输出

ac代码:


#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#define maxn 100005
using namespace std;
struct node{int weight;int speed;int order;//记录最初输入的顺序
}mice[maxn];
int dp[maxn],pre[maxn],out[maxn];
bool cmp(node a,node b)
{if(a.weight==b.weight) return a.speed>b.speed;else return a.weight<b.weight;//先以weight为标准排序
}
int main()
{int num=1,count=0,last=1;//num记录总的mouse的个数,count记录有多少只符合条件的mouseint i,j;while(scanf("%d %d",&mice[num].weight,&mice[num].speed)!=EOF){dp[num]=1;pre[num]=0;mice[num].order=num;num++;}num--;sort(mice+1,mice+1+num,cmp);//注意下标从1开始要从1开始比较,mice+1for(i=1;i<=num;i++){for(j=1;j<i;j++)//寻找speed的最大递减子序列,pd[i]记录以mice[i]结尾的最大递减子序列的长度{if(mice[i].weight>mice[j].weight && mice[i].speed<mice[j].speed && dp[i]<(dp[j]+1)){//有可能出现weight相同的情况,且要求weight一定要递增!!pre[i]=j;//记录当前子序列的倒数第二个元素dp[i]=dp[j]+1;}}if(dp[i]>count){last=i;//last记录最长递减子序列的最后一个下标(从1开始)count=dp[i];}}int t=0;while(last!=0){out[t++]=last;last=pre[last];}printf("%d\n",count);for(i=t-1;i>=0;i--)//倒序输出{printf("%d\n",mice[out[i]].order);}return 0;
}

hdoj1160:FatMouse's Speed(dp+最长递减子序列思想+数组巧妙记录输出)相关推荐

  1. NYOJ-79拦截导弹---LIS变形-最长递减子序列

    最长递增子序列 (Longest increasing subsequence) 的变形,即最长递减子序列 已知求最长上升子序列的模板为: int res = 0; for(int i =0;i< ...

  2. 算法 求一个数组的最长递减子序列 C

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! //** ...

  3. 数组的最长递减子序列java_求一个数组的最长递减子序列 比如{9,4,3,2,5,4,3,2}的最长递减子序列为{9,5,4,3,2}...

    问题描述:给出一个数列,找出其中最长的单调递减(或递增)子序列. 解题思路: 动态规划.假设0到i-1这段数列的最长递减序列的长度为s,且这些序列们的末尾值中的最大值是t.对于a[i]有一下情况: ( ...

  4. 算法 - 求一个数组的最长递减子序列(C++)

    分享一个大牛的人工智能教程.零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net /** 求一个数组的最长递减子序列 - C++ - ...

  5. 数组的最长递减子序列java_最长递增/递减子序列

    <编程之美>里有个题目是要求数组中最长递增子序列,在CSDN上看到的题目是数组中的最长递减子序列.题目如下: 求一个数组的最长递减子序列 比如{9,4,3,2,5,4,3,2}的最长递减子 ...

  6. HDU1160 FatMouse's Speed —— DP

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1160 FatMouse's Speed Time Limit: 2000/1000 MS ...

  7. HDU1159(dp最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Problem Description A subsequ ...

  8. 【HDU - 1025】Constructing Roads In JGShining's Kingdom(dp最长上升子序列模型 + 二分优化)

    题干: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  9. UVa 10066 Twin Towers (DP 最长公共子序列)

    题意  求两串数字最长公共子序列的长度 裸的lcs没啥说的 #include<cstdio> #include<cstring> #include<algorithm&g ...

  10. P1020 导弹拦截(n*log n时间的最长上升子序列思想)

    题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...

最新文章

  1. 子frame获取外部元素
  2. 32位汇编基础_内存_每个应用进程都会有自己独立的4GB内存空间
  3. Idea 七步建立Spring Mvc 的web项目,极其简单
  4. 虚拟机上无法运行Gazebo的问题?
  5. [architecture]-ARMV7架构下SecureMonitor双系统切换时保存和恢复哪些寄存
  6. java在src创建entity文件_java自动生成entity文件
  7. 从零开始学安全(三)●黑客常用的windows端口
  8. 程序员们之间的“鄙视链”,程序员底之间无声的战争
  9. 博为峰JavaEE技术文章 ——MyBatis where标签使用方法
  10. ubuntu报错 E:无法定位软件包
  11. 软件可靠性计划过程组成与LRU简介
  12. 老毛桃发帖子 去广告
  13. mac os操作系统如何降级
  14. 双线macd指标参数最佳设置_经典实用的双线MACD指标
  15. 一日一签免费算卦_一日一签app免费算卦
  16. 【优化调度】基于粒子群算法求解水火电调度优化问题含Matlab源码
  17. [Python][sklearn] 使用from sklearn.neighbors import NearestNeighbors计算相似度
  18. 如何判断linux是32位还是64位?
  19. Python脚本后台运行的几种方式
  20. stm32中断源有哪些_143条 超详细整理STM32单片机学习笔记(必看)

热门文章

  1. 关于#if NET1的一点小得
  2. 数据更新(2020-4-1)
  3. lua32位和64位字符串和Uint之间的转换
  4. 谈一下对VUE生命周期的理解
  5. java 子类型_Java – 基类和子类中的equals方法
  6. vmware+player+12+linux,Vmware player 12
  7. html获取页面input值,javascript怎么获取input中用户输入的内容?
  8. java程序设计从方法学角度描述_(特价书)Java程序设计:从方法学角度描述
  9. 你对Redis持久化了解多少?一篇文章让你明白Redis持久化
  10. 数字信号处理实验(六)——FIR滤波器的设计