#include<stdio.h> struct Mouse{ int weight; //重量 int speed; //速度 int prev; //前一节点的下标 int seq; //排序前的序号 } mice[1000] = {{0,0,-1,0}}; //比较函数 //第一关键字:weight //第二关键字:speed int compare(const void *a,const void *b){ struct Mouse *m1 = (struct mouse *)a; struct Mouse *m2 = (struct mouse *)b; if(m1->weight> m2->weight) return 1; return -1; } //递归输出 void output(int i) { if(i == -1) return; output(mice[i].prev); printf("%d/n",mice[i].seq); } int main() { int i=0, j, k; int max,index=0; int flag[1000] = {1}; //1.读入数据 while(scanf("%d%d",&mice[i].weight,&mice[i].speed) != EOF ) { mice[i].seq = i+1; //序数比下标大1 ++i; } //2. 对数据进行排序 qsort(mice, i,sizeof(struct Mouse),compare); //3. 求最长递减子序列 for( j=1; j<i; ++j){ flag[j] = 1; mice[j].prev = -1; for(k=0; k<j; ++k){ if( (mice[j].weight > mice[k].weight) && (mice[j].speed < mice[k].speed) ){ if( flag[j] <= flag[k]){ flag[j] = flag[k]+1; mice[j].prev = k; } } } } //4. 找到最大的子序列长度及其对应的flag数组的下标 max = flag[0]; for(j =0 ;j< i;++j) { if(flag[j] > max ){ max = flag[j]; index = j; } } //5. 输出:先输出总数,再输出序列 printf("%d/n",max); output(index);//输出序列各个元素未排序前的位置 return 0; }