hdu--1160--LIS+打印路径

这题做完  就去吃饭了...

快1年了 没有正常的饮食....

这题 数据蛮小的 1000可以用O(n^2)水过 而且只花了0ms

一般来说 打印路径是正序输出 而我们记录的时候都是 逆序记录的  所以 借用下stack特别好用

      touch    me

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <stack>
 4 using namespace std;
 5 
 6 const int size = 1010;
 7 struct data
 8 {
 9     int id;
10     int w;
11     int speed;
12 }mice[size];
13 int road[size];
14 int len[size];
15 stack< int >s;
16 
17 bool cmp( data p , data q )
18 {
19     if( p.w == q.w )
20         return p.speed>q.speed;
21     return p.w<q.w;     
22 }
23 
24 bool judge( data& p , data& q )
25 {
26     if( q.w>p.w && q.speed<p.speed )
27         return true;
28     return false;
29 }
30 
31 int main()
32 {
33     cin.sync_with_stdio(false);
34     int ans , pos , cnt;
35     cnt = pos = 1;
36     ans = 0;
37     while( cin >> mice[cnt].w >> mice[cnt].speed )
38     {
39         mice[cnt].id = cnt;
40         len[cnt] = 1;
41         road[cnt] = -1;
42         cnt++;
43     }
44     sort( mice+1 , mice+cnt+1 , cmp );
45     while( !s.empty() )
46         s.pop();
47     for( int i = 1 ; i<cnt ; i++ )
48     {
49         for( int j = i+1 ; j<=cnt ; j++ )
50         {
51             if( judge( mice[i] , mice[j] ) )
52             {
53                 if( len[j] < len[i]+1 )
54                 {
55                     len[j] = len[i]+1;
56                     road[j] = i;
57                 }
58                 if( len[j] > ans )
59                 {
60                     ans = len[j];
61                     pos = j;
62                 }
63             }
64         }
65     }
66     cout << ans << endl;
67     for( int i = pos ; ; i=road[i] )
68     {
69         s.push( mice[i].id );
70         if( road[i] == -1 )
71             break;
72     }
73     while( !s.empty() )
74     {
75         cout << s.top() << endl;
76         s.pop();
77     }
78     return 0;
79 }
View Code

转载于:https://www.cnblogs.com/radical/p/3853799.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值