HDU1160 FatMouse's Speed —— DP
Posted h_z_cong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU1160 FatMouse's Speed —— DP相关的知识,希望对你有一定的参考价值。
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1160
FatMouse\'s Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17386 Accepted Submission(s): 7694
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.
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.
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 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
Sample Output
4
4
5
9
7
Source
题解:
1.根据体重,先对老鼠进行升序排序。设dp[i]为老鼠i的最大排位,亦是个数。
2.对于每只老鼠i,枚举体重比它小, 速度比它快的老鼠j(由于经过了排序,所以j的范围为1~i-1),然后更新老鼠i的最大排位dp[i]。
3.dp[i]的最大值即为题目所求, 至于路径输出,设多一个pre[]数组,在跟新dp[i]的同时更新pre[i]就行了。
4.问:为什么要对老鼠进行排序,而不直接枚举呢?
答:因为对于当前老鼠i,必须先求出体重比它小, 速度比它快的老鼠j的最终dp[j],如果不经过排序, 那么老鼠j可能放在了老鼠i的后面,dp[j]还没最终确定,就要求出dp[i],这样显然是行不通的,因为dp[i]的值是在dp[j]的最终值中转移过来的。换句话说, dp[i]和dp[j]的求解顺序是有明确要求的。
5.相同类型的题目:HDU1069
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 1e3+10; 19 20 struct node 21 { 22 int wei, spd, id; 23 bool operator<(const node &x) { 24 return wei<x.wei; 25 } 26 }mice[MAXN]; 27 28 int dp[MAXN], pre[MAXN]; 29 30 void Print(int k) //输出路径 31 { 32 if(!k) return; 33 Print(pre[k]); 34 printf("%d\\n", mice[k].id); //由于经过了排序,所以输出的是原始编号。 35 } 36 37 int main() 38 { 39 int n = 0; 40 int wei, spd; 41 while(scanf("%d%d", &wei, &spd)!=EOF) 42 { 43 mice[++n].wei = wei; 44 mice[n].spd = spd; 45 mice[n].id = n; 46 } 47 48 sort(mice+1, mice+1+n); 49 mice[0].wei = -INF, mice[0].spd = INF; //!!注意边界条件 50 memset(dp, 0, sizeof(dp)); 51 memset(pre, 0, sizeof(pre)); 52 53 int k = -1; 54 for(int i = 1; i<=n; i++) 55 for(int j = 0; j<i; j++) //下标从0开始, 表明i作为第一个 56 { 57 if(mice[i].wei>mice[j].wei && mice[i].spd<mice[j].spd && dp[i]<dp[j]+1) 58 { 59 dp[i] = dp[j]+1; 60 pre[i] = j; //同时更新pre, 用于输出路径 61 } 62 if(k==-1 || dp[i]>dp[k]) 63 k = i; 64 } 65 66 printf("%d\\n", dp[k]); 67 Print(k); 68 }
以上是关于HDU1160 FatMouse's Speed —— DP的主要内容,如果未能解决你的问题,请参考以下文章
HDU 1160 FatMouse's Speed (动态规划最长下降子序列)
HDU - 1160 FatMouse's Speed(dp+路径记录)