1. 程式人生 > >HDU1160 FatMouse's Speed —— DP

HDU1160 FatMouse's Speed —— DP

size cti equal red chmod present ref fine scrip

題目鏈接: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.

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 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 Zhejiang University Training Contest 2001 題解: 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 }
View Code

HDU1160 FatMouse's Speed —— DP