HDU-1176.免费馅饼(数字三角形变形)

看到网上大多都是逆向的总结,我来搞个正向的吧...

这道题想着是和数字三角形差不多的,但是最后愣是没有写出来,感受到一股菜意......哭唧唧.jpg

  本题大意:

    给定n个序列,每个序列包含两个数表示第t s时坐标x有食物下落,初始时人在坐标为5的位置,人每秒只能移动一个单位,当所有食物下落后,问人能捡到的最大食物数。

  本题思路:

    和数字三角形是一个思路的问题,很容易可以推导出状态转移方程为dp[i][j] += maxx(dp[i - 1][j - 1], dp[i - 1][j], dp[i - 1][j + 1]),好吧我承认很简单我很笨....

  参考代码:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int maxn = 1e5 + 5;
 7 int n, x, t, ans, maxt;
 8 int dp[maxn][12];
 9 
10 int maxx(int a, int b, int c) {
11     int cnt = a > b ? a : b;
12     cnt = cnt > c ? cnt : c;
13     return cnt;
14 }
15 
16 int main () {
17     while(cin >> n && n) {
18         maxt = ans = 0;
19         memset(dp, 0, sizeof dp);
20         for(int i = 0; i < n; i ++) {
21             cin >> x >> t;
22             if(maxt < t) maxt = t;
23             dp[t][x] ++;
24         }
25         for(int i = 2; i <= maxt; i ++) {
26             for(int j = 0; j < 11; j ++) {
27                 dp[i][j] += maxx(dp[i - 1][j - 1], dp[i - 1][j], dp[i - 1][j + 1]);
28                 ans = max(ans, dp[i][j]);
29             }
30         }
31         printf("%d\n", ans);
32     }
33     return 0;
34 }
View Code

...有大佬可能会说,辣鸡,我的maxx是max(max())....呜呜呜,我很辣鸡.jpg

posted @ 2019-04-02 20:55  Cruel_King  阅读(250)  评论(0编辑  收藏  举报