【最长上升子序列记录路径(n^2)】HDU 1160 FatMouse's Speed

https://vjudge.net/contest/68966#problem/J

【Accepted】

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<cmath>
 6 #include<algorithm>
 7 
 8 using namespace std;
 9 const int maxn=1e3+3;
10 struct node
11 {
12     int w;
13     int s;
14     int id;
15 }a[maxn];
16 int pre[maxn];
17 int dp[maxn];
18 bool cmp(node x,node y)
19 {
20     return x.w<y.w;
21 }
22 
23 void Print(int pos)
24 {
25     if(pos==-1) return;
26     Print(pre[pos]);
27     printf("%d\n",a[pos].id);
28 }
29 int main()
30 {
31     int cnt=0;
32     while(scanf("%d%d",&a[cnt].w,&a[cnt].s)!=EOF)
33     {
34         a[cnt].id=cnt+1;
35         cnt++; 
36     }
37     sort(a,a+cnt,cmp); 
38     memset(pre,-1,sizeof(pre));
39     fill(dp,dp+cnt,1);
40     for(int i=0;i<cnt;i++)
41     {
42         for(int j=0;j<i;j++)
43         {
44             if(a[j].w<a[i].w&&a[j].s>a[i].s)
45             {
46                 if(dp[j]+1>dp[i])
47                 {
48                     dp[i]=dp[j]+1;
49                     pre[i]=j;
50                 }
51             }
52         }
53     }
54     int cou=1;
55     int pos=-1;
56     for(int i=0;i<cnt;i++)
57     { 
58         if(dp[i]>cou)
59         {
60             cou=dp[i];
61             pos=i;
62         }
63     }
64     if(cou==1)
65     {
66         printf("%d %d\n",1,1);
67         return 0;
68     }
69     cout<<cou<<endl;
70     Print(pos);
71     return 0;    
72 } 
View Code

【教训】

while(scanf("%d%d",&a[cnt].w,&a[cnt].s)!=EOF)
    {
        a[cnt].id=++cnt;
    }

是不对的,先算++cnt,前面的cnt已经加一了

 

posted @ 2017-07-23 19:16  shulin15  阅读(185)  评论(0编辑  收藏  举报