忙活了一个晚上终于完成了。。。
就是最长上升子序列的变形版。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{
int weight;
int speed;
int i;
int mark;
int father;
}node;
node mice[1002];
int cmp(const void *a,const void *b)
{
return (*(node*)b).speed-(*(node*)a).speed;
}
int main()
{
int t,k,h,tmp;
int result[1001];
int max,kmax;
t=0;kmax=-100;
while(scanf("%d%d",&mice[t].weight,&mice[t].speed)!=EOF)
{
mice[t].i=t+1;
t++;
}
qsort(mice,t,sizeof(node),cmp);
mice[0].mark=1;mice[0].father=0;
for(k=1;k<t;k++)
{
max=-1;
for(h=0;h<=k-1;h++)
{
if(mice[k].weight>mice[h].weight&&mice[h].mark>max)
{
max=mice[h].mark;
mice[k].father=h;
}
}
if(max>kmax)kmax=max;
if(max==-1)
mice[k].mark=1;
else
mice[k].mark=max+1;
}
kmax += 1;
for(tmp=t-1;tmp>=0;tmp--)
if(mice[tmp].mark==kmax)break;
h=kmax;k=0;
printf("%d\n",kmax);
while(h--)
{
result[k]=mice[tmp].i;k++;
tmp=mice[tmp].father;
}
for(h=kmax-1;h>=0;h--)
printf("%d\n",result[h]);
return 0;
}