您的位置:首页 > 其它

hdu 1160 FatMouse's Speed

2017-05-14 13:34 218 查看
题目链接:点击打开链接

题意:简单dp,最长递增/减子序列的变形。找到一个序列,使老鼠的体重递增但老鼠的速度递减,注意:此处的序列没要求是按照给出的相对顺序,所以在处理数据前,先按照体重递增速度递减排序,便于处理。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct node{
int index,weight,speed;
}a[1005];
int dp[1005],pre[1005];//pre用于打印路径
bool cmp(node& b,node& c){
if(b.weight!=c.weight){
return b.weight<c.weight;
}
else{
return b.speed>c.speed;
}
}
void printPath(int loc){
if(pre[loc]!=-1){
printPath(pre[loc]);
}
printf("%d\n",a[loc].index);
}
int main(){
int c,d,end,cnt=0,M=-1;
memset(pre,-1,sizeof(pre));
while(scanf("%d%d",&c,&d)!=EOF){//读取数据
cnt++;
a[cnt].weight=c;
a[cnt].speed=d;
a[cnt].index=cnt;
dp[cnt]=1;
};
sort(a+1,a+cnt+1,cmp);//排序
for(int i=2;i<=cnt;i++){//dp 复杂度O(n^2)
for(int j=i-1;j>=1;j--){
if(a[i].weight>a[j].weight&&a[i].speed<a[j].speed&&dp[i]<dp[j]+1){
dp[i]=dp[j]+1;
pre[i]=j;
}
}
if(dp[i]>M){
end=i;
M=dp[i];
}
}
if(dp[end]==1){
printf("0\n");
}
else{
printf("%d\n",dp[end]);
printPath(end);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: