【DP|LIS+输出路径】HDU-1160 FatMouse's Speed

来源:互联网 发布:数据透视表 总计 编辑:程序博客网 时间:2024/05/16 01:46

FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Special Judge

Problem Description
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 speeds are decreasing.
 

Input
Input contains data for a bunch of mice, one mouse per line, terminated by end of file.

The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.

Two mice may have the same weight, the same speed, or even the same weight and speed. 
 

Output
Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],..., m[n] then it must be the case that 

W[m[1]] < W[m[2]] < ... < W[m[n]]

and 

S[m[1]] > S[m[2]] > ... > S[m[n]]

In order for the answer to be correct, n should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one. 
 

Sample Input
6008 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900
 

Sample Output
44597
 
————————————————————————————————————————————————————————
思路:很明显就是LIS,只不过需要输出路径而已。输出路径本来就是增加一个fa[]数组来记录的事,结果我做了那么久想不到该怎么办。下次记住,输出路径就是需要fa[]数组。
另外值得学习是,DP这个过程实际上就像是在一个二维数组上面填写数字。如果一行一行的看的话,下一行就要找到上一行的最佳位置转移过来。最优解的子状态就隐藏在当前行当中。直到填完最后一行,就可以得到最优解。
(如果需要输出路径当然还需要记录一下最优解的位置)
dp[i][j] = max(dp[i-1][0...j]) + 1
P.S. 本题因为转移的时候i只是从i-1转移过来,可以使用滚动数组。
代码如下:
/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <climits>#include <iostream>#define For(i,x,y) for(int i = x; i < y; i++)#define Mem(f,x) memset(f, x, sizeof(f))#define Sca(x) scanf("%d", &x)#define Pri(x) printf("%d\n", x)#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;/****************************************/const int N = 1111;struct Node {int wei, spe, num;}mice[N];int fa[N], dp[N];bool cmp(Node a, Node b){if(a.wei == b.wei) return a.spe > b.spe;return a.wei < b.wei;}void dfs(int u){if(u == -1) return ;dfs(fa[u]);printf("%d\n", mice[u].num);}int main(){#ifdef J_Surefreopen("000.in", "r", stdin);freopen("999.out", "w", stdout);#endifint cnt = 0;while(~scanf("%d%d", &mice[cnt].wei, &mice[cnt].spe)) {mice[cnt].num = cnt+1;fa[cnt] = -1;cnt++;}sort(mice, mice+cnt, cmp);dp[0] = 1;int ans = 0, v;For(i, 1, cnt) {int len = 0;For(j, 0, i) {if(mice[j].wei < mice[i].wei && mice[j].spe > mice[i].spe) {if(len < dp[j]) {len = dp[j];fa[i] = j;}//状态转移的时候还要记得顺便记录路径}}dp[i] = len + 1;if(ans < dp[i]) {ans = dp[i];v = i;}//最优解改变的时候还要记得顺便改变最优解所在位置}printf("%d\n", ans);dfs(v);return 0;}


0 0
原创粉丝点击