题目大意:要证明老鼠越胖,跑的越慢。求最长的证明证据。每一行输入老鼠体重,速度,以文件结束跳出。
解题思路:将所有输入的老鼠,按输入顺序编号。然后按体重重优先与速度慢(体重轻优先与速度快)排序。然后用类似HDU1069长宽比较的方式,只是这里是,体重大于他之前,且速度慢与他之前,且只要算数量,但是要pre标记前一个编号为几。用于最后输出。
ac代码:
#include <iostream>
#include <algorithm>
using namespace std;
struct node{
int num;
int weight;
int speed;
}mice[1005];
int cnt=0, dp[1005], pre[1005], Max, temp, a[1005];
bool compare(node a, node b)
{
if (a.weight != b.weight)
return a.weight < b.weight;
if (a.speed != b.speed)
return a.speed > b.speed;
return a.num < b.num;
}
int main()
{
while (scanf("%d%d", &mice[cnt].weight, &mice[cnt].speed)!=EOF)
mice[cnt].num = cnt + 1, dp[cnt++] = 1;
sort(mice, mice+cnt, compare);
for (int i=0; i<cnt; i++){
pre[mice[i].num] = mice[i].num;
for (int j=i-1; j>=0; j--)
if (mice[j].weight < mice[i].weight && mice[j].speed > mice[i].speed){
if (dp[i] < dp[j]+1)
pre[mice[i].num] = mice[j].num;
dp[i] = max(dp[i], dp[j]+1);
}
}
Max = dp[0];
temp = mice[0].num;
for (int i=1; i<cnt; i++)
if (Max < dp[i]){
Max = dp[i];
temp = mice[i].num;
}
printf("%d\n", Max);
for (int i=0; i<Max; i++){
a[i] = temp;
temp = pre[temp];
}
for (int i=0; i<Max; i++)
printf("%d\n", a[Max-i-1]);
return 0;
}