码迷,mamicode.com
首页 > 其他好文 > 详细

[HDOJ1160]FatMouse's Speed(DP)

时间:2016-04-16 12:19:28      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160

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.

给一组w s,找出一个最长的序列,使得w是严格单调递增而且s是严格单调递减的。

两种做法,分别是关注w和关注s。

 

 

关注w:相当于是在s上求最长下降子序列。

技术分享
 1 #include <algorithm>
 2 #include <iostream>
 3 #include <iomanip>
 4 #include <cstring>
 5 #include <climits>
 6 #include <complex>
 7 #include <fstream>
 8 #include <cassert>
 9 #include <cstdio>
10 #include <bitset>
11 #include <vector>
12 #include <deque>
13 #include <queue>
14 #include <stack>
15 #include <ctime>
16 #include <set>
17 #include <map>
18 #include <cmath>
19 
20 using namespace std;
21 
22 typedef struct Node {
23     int w;
24     int s;
25     int idx;
26 }Node;
27 
28 const int maxn = 1111;
29 Node mice[maxn];
30 int n, ans;
31 int dp[maxn];
32 int pre[maxn];
33 int st[maxn];
34 int top;
35 
36 bool cmp(Node a, Node b) {
37     if(a.w == b.w) return a.s > b.s;
38     return a.w < b.w;
39 }
40 
41 int main() {
42     // freopen("in", "r", stdin);
43     // freopen("out", "w", stdout);
44     n = 1;
45     while(~scanf("%d %d", &mice[n].w, &mice[n].s)) {
46         mice[n].idx = n;
47         n++;
48     }
49     n--;
50     sort(mice + 1, mice + n + 1, cmp);
51     ans = 0;
52     int pos;
53     memset(dp, 0, sizeof(dp));
54     memset(pre, -1, sizeof(pre));
55     for(int i = 1; i <= n; i++) {
56         dp[i] = 1;
57         for(int j = 1; j < i; j++) {
58             if(dp[i] < dp[j] + 1 &&
59                 mice[i].s < mice[j].s &&
60                 mice[i].w > mice[j].w) {
61                 dp[i] = dp[j] + 1;
62                 pre[mice[i].idx] = mice[j].idx;
63             }
64         }
65         if(ans < dp[i]) {
66             ans = dp[i];
67             pos = mice[i].idx;
68         }
69     }
70     top = 0;
71     for(int i = pos; ~i; i=pre[i]) st[top++] = i;
72     printf("%d\n", ans);
73     while(top) printf("%d\n", st[--top]);
74     return 0;
75 }
View Code

 

关注s:相当于是在w上求最长上升子序列。

技术分享
 1 #include <algorithm>
 2 #include <iostream>
 3 #include <iomanip>
 4 #include <cstring>
 5 #include <climits>
 6 #include <complex>
 7 #include <fstream>
 8 #include <cassert>
 9 #include <cstdio>
10 #include <bitset>
11 #include <vector>
12 #include <deque>
13 #include <queue>
14 #include <stack>
15 #include <ctime>
16 #include <set>
17 #include <map>
18 #include <cmath>
19 
20 using namespace std;
21 
22 typedef struct Node {
23     int w;
24     int s;
25     int idx;
26 }Node;
27 
28 const int maxn = 1111;
29 Node mice[maxn];
30 int n, ans;
31 int dp[maxn];
32 int pre[maxn];
33 int st[maxn];
34 int top;
35 
36 bool cmp(Node a, Node b) {
37     if(a.s == b.s) return a.w < b.w;
38     return a.s > b.s;
39 }
40 
41 int main() {
42     // freopen("in", "r", stdin);
43     // freopen("out", "w", stdout);
44     n = 1;
45     while(~scanf("%d %d", &mice[n].w, &mice[n].s)) {
46         mice[n].idx = n;
47         n++;
48     }
49     n--;
50     sort(mice + 1, mice + n + 1, cmp);
51     ans = 0;
52     int pos;
53     memset(dp, 0, sizeof(dp));
54     memset(pre, -1, sizeof(pre));
55     for(int i = 1; i <= n; i++) {
56         dp[i] = 1;
57         for(int j = 1; j < i; j++) {
58             if(dp[i] < dp[j] + 1 &&
59                mice[i].w > mice[j].w) {
60                 dp[i] = dp[j] + 1;
61                 pre[mice[i].idx] = mice[j].idx;
62             }
63         }
64         if(ans < dp[i]) {
65             ans = dp[i];
66             pos = mice[i].idx;
67         }
68     }
69     top = 0;
70     for(int i = pos; ~i; i=pre[i]) st[top++] = i;
71     printf("%d\n", ans);
72     while(top) printf("%d\n", st[--top]);
73     return 0;
74 }
View Code

 

注意小心关注w时s相等的情况和关注s时w相等的情况。不知道为什么,关注w时没有注意s相等的代码可以AC但是关注s的时候必须要注意w相等要continue掉。应该是数据水了吧。

[HDOJ1160]FatMouse's Speed(DP)

标签:

原文地址:http://www.cnblogs.com/vincentX/p/5398026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!