FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6861 Accepted Submission(s): 2996
Special Judge
but the speeds are decreasing.
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.
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.
6008 1300 6000 2100 500 2000 1000 4000 1100 3000 6000 2000 8000 1400 6000 1200 2000 1900
4 4 5 9 7
思路就是:先按体重递增进行sort排序,然后按照体重找到最长递减子序列即可,用动态规划做比较简单。状态f[i]表示前i个老鼠中的最长递减子序列长度,状态转移方程为f[i] = max{f[j], mice[j].speed > mice[i].speed} + 1, 最后找出最大的f[i]即可。注意此题还需要输出找到的序列中的老鼠的最原始的标号,因此不仅要在刚开始的时候把每个老鼠的最初的序号记下来,还要在进行状态转移的时候把当前的老鼠的位置标记下来。
#include<stdio.h> #include<algorithm> #include<string.h> using namespace std; int i,j; int pre[1007],l[1007],ans[1007];//ans记录路径 struct node{ int weight; int speed; int num; }mouse[10007]; int cmp(const node &a,const node &b) { if(a.weight!=b.weight) return a.weight<b.weight; else return a.speed>b.speed; } int main() { int cnt=0; while(scanf("%d%d",&mouse[cnt].weight,&mouse[cnt].speed)!=EOF&&mouse[cnt].weight) { mouse[cnt].num=cnt++; } sort(mouse,mouse+cnt,cmp); for(i=1;i<cnt;i++) { pre[i]=0;//记录路径 l[i]=1;//记录长度 //printf("%d\n",mouse[i].weight); } for(i=1;i<cnt;i++) { for(j=1;j<i;j++) { if(mouse[i].weight>mouse[j].weight&&mouse[i].speed<mouse[j].speed) { if(l[i]<l[j]+1) { l[i]=l[j]+1; pre[i]=j; } } } } int end=1,max=l[1]; for(i=2;i<cnt;i++) { if(max<l[i]) { max=l[i]; end=i; } } printf("%d\n",max); for(i=1;i<cnt;i++) { ans[i]=end; end=pre[end]; } for(i=max;i>=1;i--) { //printf("%d %d\n",mouse[ans[i]].weight,mouse[ans[i]].speed); printf("%d\n",mouse[ans[i]].num+1); } //printf("%d\n",mouse[ans[1]].num); return 0; }