hdu 1160 FatMouse's Speed

来源:互联网 发布:gta5女性捏脸数据可爱 编辑:程序博客网 时间:2024/05/17 21:48

题目链接:点击打开链接

题意:简单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;}

1 0