Total Submission(s): 7133 Accepted Submission(s): 3139
Special Judge
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.
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
解题思路:在体重严格递增的情况下,要求速度严格递减的老鼠有几只,还要输出他们出现的顺序(按输入时顺序值)。
在体重与速度递减排序后,加条件求最大严格递增子序列,记录路径,后面直接输出。
求最大子序列(http://blog.csdn.net/lsh670660992/article/details/9498833)
#include<cstdio> #include<algorithm> using namespace std; struct node //老鼠的属性 { int w; //体重 int s; //速度 int x; //输入时的顺序号 }mice[1005]; int cmp(node a,node b) //以体重为标准,将老鼠按体重递减排序 { return a.w<b.w; } int main() { int n=0; int i,j; int much[1005],head[1005]; //分别记录第i只老鼠为止,子序列长度与前驱存储位置(mice数组中下标) while(scanf("%d%d",&mice[n].w,&mice[n].s)!=EOF) mice[n].x=n+1,n++; sort(mice,mice+n+1,cmp); //排序 head[0]=-1; //终止标记 much[0]=1; int max1=0,x; //由第一只老鼠到当前检索位置为止,子序列的最大长度和其前驱位置在前驱记录数组head中的下标 for(i=1;i<=n;i++) { head[i]=-1; much[i]=1; for(j=0;j<i;j++) { if(mice[i].w>mice[j].w&&mice[i].s<mice[j].s&&much[j]+1>much[i]) //体重严格递增,速度严格递减,子序列更长 { much[i]=much[j]+1; head[i]=j; if(much[i]>max1) //前驱处理 { max1=much[i]; x=i; } } } } int road[1005]; i=0; while(1) //处理路劲记录 { road[i++]=mice[x].x; x=head[x]; if(x==-1) break; } printf("%d\n",max1); for(i--;i>=0;i--) printf("%d\n",road[i]); return 0; }