//Accepted 1160 15MS 264K 1027 B C++
//dp题 转化为求最长子序列问题
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
struct node{
int weight;
int speed;
int order;
};
node mice[1002];
bool cmp(node a,node b)
{
if(a.weight != b.weight)
return a.weight < b.weight;
else
return a.speed > b.speed;
}
int b = 1,f[1002],d[1002],res[1002];
int main()
{
int w,s,n,size;
int i,j,num;
size = 1;
while(scanf("%d%d",&mice[b].weight,&mice[b].speed)!= EOF)
{
mice[b].order = b;
b++;
}
sort(&mice[1],&mice[1] + b - 1,cmp);
d[1] = 1;
num = 1;
for(i = 1; i < b; i++)
{
f[i] = 1;
for(j = 1; j < i; j++)
{
if(f[j] + 1 > f[i] && (mice[j].weight < mice[i].weight && mice[j].speed > mice[i].speed))
{
f[i] = f[j] + 1;
d[i] = j;
if(f[i] >= size)
{
size = f[i];
num = i;
}
}
}
}
b = size;
for(i = num; i >= 1; i = d[i])
{
res[b--] = mice[i].order;
}
printf("%d/n",size);
for(i = 1; i <= size; i++)
{
printf("%d/n",res[i]);
}
}
hdu 1160 DP 最长子序列问题
最新推荐文章于 2022-02-25 21:57:21 发布