hdu 1176 免费馅饼

中文题面: http://acm.hdu.edu.cn/showproblem.php?pid=1176

思路: 这是个二维dp dp[t][x] 前者表示时间,后者表示坐标

那么转移方程就是 dp[t][x] = max(dp[t-1][x], dp[t-1][x+1], dp[t-1][x - 1]) + cnt[t][x]; (注意边界)

cnt[t][x]表示t时刻x位置掉的馅饼个数

 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stdio.h>
 5 #include <map>
 6 #include <queue>
 7 #include <vector>
 8 using namespace std;
 9 
10 int dp[100005][12];
11 int cnt[100005][12];
12 
13 int main() {    
14     int n; 
15     while (scanf("%d", &n) == 1 && n) {
16         int mint = 200000, maxt = 0;
17         memset(cnt, 0, sizeof cnt);
18         for (int i = 1; i <= n; ++i) {
19             int xt, t;
20             scanf("%d%d", &xt, &t);
21             ++cnt[t][xt];
22             if (t > maxt) maxt = t;
23         }
24         memset(dp, 0, sizeof dp);
25         int ans = 0;
26         dp[1][4] = cnt[1][4];
27         dp[1][5] = cnt[1][5];
28         dp[1][6] = cnt[1][6];
29         for (int t = 2; t <= maxt; ++t) {
30             dp[t][0] = max(dp[t-1][0], dp[t-1][1]) + cnt[t][0];
31             for (int x = 1; x <= 9; ++x) {
32                 int t1 = max(dp[t - 1][x - 1], dp[t - 1][x + 1]);
33                 dp[t][x] = max(dp[t - 1][x], t1) + cnt[t][x];
34             }
35             dp[t][10] = max(dp[t-1][10], dp[t-1][9]) + cnt[t][10];
36         }
37         for (int x = 0; x <= 10; ++x)
38             ans = max(ans, dp[maxt][x]);
39         printf("%d
", ans);
40     }
41     return 0;
42 }
原文地址:https://www.cnblogs.com/boson-is-god/p/6682964.html