由最长递增子序列引申而来,先按体重进行排序。
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
int index;
int w;
int iq;
};
struct node ele[1001];
int length;
int dp[1001], pre[1001];
int cmp(const void *a, const void *b)
{
struct node *c = (node *)a;
struct node *d = (node *)b;
if(c->w != d->w)
return c->w - d->w;
else
return d->iq - c->iq;
}
int main()
{
//freopen("11.6.1.txt", "r", stdin);
int i = 0, j, max = 0, re, k;
int result[1001];
while(scanf("%d %d", &ele[i].w, &ele[i].iq) != EOF){
ele[i].index = i+1;
i++;
}
length = i;
qsort(ele, length, sizeof(ele[0]), cmp);
dp[0] = 1;
pre[0] = -1;
for(i = 1; i < length; i++){
//
dp[i] = 1;
pre[i] = -1;
for(j = 0; j < i; j++){
if(ele[i].w > ele[j].w && ele[i].iq < ele[j].iq && dp[i] < dp[j] + 1){
dp[i] = dp[j] + 1;
pre[i] = j;
}
} //end for j
} //end for i
for(i = 0; i < length; i++){
if(dp[i] > max){
max = dp[i];
re = i;
}
}
printf("%d\n", max);
k = re;
j = max-1;
while(k){
result[j] = ele[k].index;
j--;
k = pre[k];
}
//输出结果
for(i = 0; i < max; i++){
printf("%d\n", result[i]);
}
return 0;
}