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 }

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

时间: 2024-08-27 08:37:17

hdu--1160--LIS+打印路径的相关文章

Uva 10131 Is Bigger Smarter? (LIS,打印路径)

option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意:给定若干大象的体重及智商值.求满足大象体重严格递增,智商严格递减的序列的最大个数. 并打印随意一组取得最大值的序列的大象编号 分析:这个是LIS的应用,仅仅只是推断条件有两个,能够先对大象的体重排序,可是要打印路径. 那就必须得回溯求路径.能够直接逆序循环求,当然递归也是一个好的选择 #include<

What Goes Up UVA - 481 LIS+打印路径 【模板】

打印严格上升子序列: #include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<qu

LIS [HDU 1160] FatMouse&#39;s Speed

FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9448    Accepted Submission(s): 4205 Special Judge Problem Description FatMouse believes that the fatter a mouse is, the faster

HDU - 1160 FatMouse&#39;s Speed(dp+路径记录)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意:给定x组老鼠的重量(W)和速度(S).求使得   W[m[1]] < W[m[2]] < ... < W[m[n]]       S[m[1]] > S[m[2]] > ... > S[m[n]] 的最长序列长度和路径 题解:排序一下,然后LIS,路径记录一下,输出就可以了 1 #include <iostream> 2 #include <a

HDU 1385 Minimum Transport Cost(Floyd 最短路 打印路径)

HDU 1385 大意: 有N个城市,然后直接给出这些城市之间的邻接矩阵,矩阵中-1代表那两个城市无道路相连,其他值代表路径长度. 如果一辆汽车经过某个城市,必须要交一定的钱(可能是过路费). 现在要从a城到b城,花费为路径长度之和,再加上除起点与终点外所有城市的过路费之和. 求最小花费,如果有多条路经符合,则输出字典序最小的路径. 思路: Floyd求最短路,打印路径即可. 1 /*--------------------------------------------------------

HDU 1160 FatMouse&#39;s Speed 动态规划 记录路径的最长上升子序列变形

题目大意:输入数据直到文件结束,每行两个数据 体重M 和 速度V,将其排列得到一个序列,要求为:体重越大 速度越低(相等则不符合条件).求这种序列最长的长度,并输出路径.答案不唯一,输出任意一种就好了. 题目思路:这是个最长上升子序列的问题,我们按W的升序进行排序,若W相等则按V的降序排序.用Pre[]记录当前点的前驱节点,Last记录序列最后一个点,maxn记录最长长度,完成动规后可根据Last和Pre[]输出路径. #include<cstdio> #include<stdio.h&

hdu 1160 FatMouse&#39;s Speed(最长不下降子序列+输出路径)

题意: FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the s

UVa 103 - Stacking Boxes (LIS,打印路径)

链接:UVa 103 题意:给n维图形,它们的边长是{d1,d2,d3...dn},  对于两个n维图形,求满足其中一个的所有边长 按照任意顺序都一一对应小于另一个的边长,这样的最长序列的个数,并且打印任意一个最长子串的路径, 例如:a(9,5,7,3),b(6,10,8,2),c(9,7,5,1),a和b不满足,但c和b满足 分析:首先对没组边长从小到大排序,再对各组图形按最小边排序,再求最大子串, 对于打印路径,可以逆序循环,也可递归求解 #include<cstdio> #include

FatMouse&#39;s Speed hdu 1160(动态规划,最长上升子序列+记录路径)

http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意:现给出老鼠的体重与速度,要求你找出符合要求的最长子序列.       要求是 W[m[1]] < W[m[2]] < ... < W[m[n]](体重) && S[m[1]] > S[m[2]] > ... > S[m[n]] (速度) 分析:有两个变量的话比较不好控制,自然需要先排序.再仔细思考的话,觉得和之前做的防御导弹有点类似,都是求最多能有几个符合

HDU 1160 FatMouse&#39;s Speed (最长上升子序列+记录路径)

题目链接:HDU 1160 FatMouse's Speed 题意:求体重越重,反而速度越慢的例子,并输出对应的编号. 对speed进行从大到小排序,再求weight的最长上升序列,并输出路径. AC代码: #include<stdio.h> #include<algorithm> #include<stack> using namespace std; struct Node { int weight; int speed; int id; }; struct Nod