HDU FatMouse's Speed 基本DP

题意:要求找到的体重递增,速度递减的老鼠,并且输出最长的长度数,而且输出各自的序列数。Special Judge 

思路:先按体重由小到大排序,再找最长速度递减序列。

转移方程:mou[i].w>mou[j].w&&mou[i].s<mou[j].s&&dp[j]+1>dp[i]

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<iostream>
 5 #define clc(a,b) sizeof(a,b,sizeof(a))
 6 #define LL long long
 7 #include<cmath>
 8 using namespace std;
 9 int dp[1010];//表示以i结尾的最长长度
10 int rightt[1010];//最终输出序列
11 int pre[1010];//记录i的前驱是什么
12 struct node {
13     int w,s,index;
14 } mou[1010];
15 
16 bool cmp(node a,node b) {
17     if(a.w!=b.w) return a.w<b.w;
18     else return a.s>b.s;
19 }
20 
21 int main() {
22 //    freopen("in.txt","r",stdin);
23 //    freopen("out.txt","w",stdout);
24     int k=1;
25     while(scanf("%d%d",&mou[k].w,&mou[k].s)!=EOF) {
26         mou[k].index=k;
27         k++;
28     }
29     sort(mou+1,mou+k,cmp);
30     clc(dp,1);
31     clc(rightt,0);
32     clc(pre,0);
33     int tot=0;
34     int last;
35     for(int i=1; i<k; i++) {
36         for(int j=1; j<i; j++) {
37             if(mou[i].w>mou[j].w&&mou[i].s<mou[j].s&&dp[j]+1>dp[i]) {
38                 dp[i]=dp[j]+1;
39                 pre[i]=j;
40                 if(tot<dp[i]) {
41                     tot=dp[i];
42                     last=i;
43                 }
44             }
45         }
46     }
47     int r=last;
48     int i=0;
49     while(r!=0) {
50         rightt[i++]=r;
51         r=pre[r];
52     }
53     printf("%d
",i);
54     for(int j=i-1; j>=0; j--) {
55         printf("%d
",mou[rightt[j]].index);
56     }
57     return 0;
58 }
View Code
原文地址:https://www.cnblogs.com/ITUPC/p/5283465.html