InputInput 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.
OutputYour 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 题意:每行读入两个数,一个是老鼠体重,一个是老鼠速度,输入到文件结束,输出n,再输出n个老鼠的编号,表示这n只老鼠按照速度递减,体重递增的顺序排列。 思路:在读入结束以后,在普通的lis上多加两个数组,一个记录当前路径,另个一记录最终路径,输出over. 总结: 翻译就是一个坑,题目上都说了,if these ...are,说明题目只是举个栗子,而不是真的按照m[1],m[2]编号啊,好,翻译over; 再看怎么输入的问题,我一开始用一个变量sum记录读入的老鼠个数,也就是scanf("%d%d",&vis[sum].weight,&vis[sum++].speed),这样同样被坑啊,我一开始死活认为是结构体排序出问题了, 后来排查才发现读入得用两个变量分别记录老鼠个数,虽然它们的值始终保持相同,但是这样就是正确的啊,我研究了半天还是不知道为什么,先留着,下午再来解决,输入over. 最后一个坑,以后自己别偷懒,赋值的时候写什么trace1[i].num[k] = trace1[j].num[k++];exo???求求我自己下次勤快一点把k++换行写好吗,你知不知道我花了多少时间才找出这样的bug!! 这道题不难,如果自己不擅长写bug,很快就能AC,BTW,我从8.30写到了11.30,中间no take a rest,莫名被自己菜哭!!!!
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; #define N 1000 int dp[N+10],trace[N+10]; struct node1{ int weight,speed,number; }vis[N+10];//记录老鼠信息 struct node2{ int num[N]; }trace1[N+10];//记录临时路径 int cmp(struct node1 a,struct node1 b) { if(a.speed == b.speed ) return a.weight > b.weight ; return a.speed > b.speed ; } int main() { int i,j,k,sum1,sum2,maxl,sum; memset(dp,0,sizeof(dp)); sum1 = sum2 = 1; maxl = -1; while(scanf("%d%d",&vis[sum1++].weight,&vis[sum2++].speed)!=EOF)//读入不要用同一个变量进行记录,否则出错 { sum = sum1; } for(i = 1; i < sum; i ++) vis[i].number = i;//老鼠编号 sort(vis+1,vis+sum,cmp);//速度递增 trace1[1].num[1] = vis[1].number;//长为1的递增序列起点是本身。 dp[1] = 1;//最长序列初始化为1 for(i = 2; i < sum; i ++) { for(j = 1; j < i; j ++) { k = 1; if(vis[i].weight > vis[j].weight )//体重递减 { if(dp[j] > dp[i]) { dp[i] = dp[j]; while(k <= dp[j])//记录临时路径 { trace1[i].num[k] = trace1[j].num[k]; k ++; } } } } dp[i] += 1;//加上本身 trace1[i].num[dp[i]] = vis[i].number ;//自身的编号位于路径最末端 if(dp[i] > maxl)//如果有临时路径大于最大路径长度,更新 { maxl = dp[i]; k = 1; while(k <= maxl) { trace[k] = trace1[i].num[k]; k++; } } } printf("%d\n",maxl); k = 1; while(k <= maxl) { printf("%d\n",trace[k]); k++; } return 0; }