UVA_10131
这个题目首先可以把大象按体重升序排列,然后求一个IQ的最长下降子序列即可。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXD 1010
int N, w[MAXD], q[MAXD], f[MAXD], r[MAXD], p[MAXD];
int cmp(const void *_p, const void *_q)
{
int *p = (int *)_p;
int *q = (int *)_q;
return w[*p] - w[*q];
}
void printresult(int i)
{
if(p[i] != -1)
printresult(p[i]);
printf("%d\n", r[i] + 1);
}
void solve()
{
int i, j, k, max;
for(i = 0; i < N; i ++)
{
r[i] = i;
f[i] = 1;
}
memset(p, -1, sizeof(p));
qsort(r, N, sizeof(r[0]), cmp);
for(i = 1; i < N; i ++)
for(j = 0; j < i; j ++)
{
if(w[r[j]] == w[r[i]])
break;
if(q[r[j]] > q[r[i]] && f[j] + 1 > f[i])
{
f[i] = f[j] + 1;
p[i] = j;
}
}
max = 0;
for(i = 0; i < N; i ++)
if(f[i] > max)
{
max = f[i];
k = i;
}
printf("%d\n", max);
printresult(k);
}
int main()
{
N = 0;
while(scanf("%d%d", &w[N], &q[N]) == 2)
N ++;
solve();
return 0;
}