FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14386 Accepted Submission(s): 6346
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.
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. //只用输出一个
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题目意思:就是要推翻题中资料给的假设,那就要找老鼠体型是递增而老鼠跑的速度是递减的这种最长的序列。解题思路:动态规划题目,先将老鼠的体重进行正排序,然后求速度的最长单调递减子序列。或将老鼠的体重按从大到小排序,然后求速度的最长单调递增子序列。
#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int dp[10000];//dp[K]代表前K个元素组成的最长单调递增(递减)序列的长度struct mice{int ssize;//老鼠的体型int speed;//老鼠的速度int num;//老鼠的编号}a[10000];int cmp(mice a,mice b)//按老鼠的大小进行正排序{return (a.ssize<b.ssize);}int main(){int i=0,j,k,si,sp,p[10000];while(scanf("%d%d",&si,&sp)!=EOF){a[i].ssize=si;a[i].speed=sp;a[i].num=i+1;i++;}sort(a,a+i,cmp);//按老鼠的体型进行正排序int len=0;for(j=0;j<i;j++){dp[j]=1;//最长单调递减子序列的长度最少为1for(k=0;k<j;k++)//{if(a[k].ssize<a[j].ssize&&a[k].speed>a[j].speed)//如果体型小于当前老鼠的体型,而且速度大于当前老鼠的速度{if(dp[k]+1>dp[j])//dp[k]前k个元素所组成的最长单调递减子序列的长度。加1是带上a[j],看是否比dp[j]大,如果//大dp[j]被刷新。dp[j]=dp[k]+1;}}if(dp[j]>len)//不断刷新最大长度。len=dp[j];}printf("%d\n",len); //到此为止是求了最长递减子序列的长度,此时dp里面已经存了当前位置的最长递减子序列的长度j=0;for(k=i-1;k>=0;k--) //我们就从后往前找,每找到一个,就让最大长度-1,{if(len==dp[k]){p[j++]=a[k].num;len--;}}for(i=j-1;i>=0;i--) //最后输出,题目说了,答案可能有多个,只用输出一个即可。printf("%d\n",p[i]);return 0;}