hdu1176 免费馅饼 ——DP

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

题目大意:

  中文题……

题目思路:

  类似于 Triangle 。d[i][j] 表示 i 时间在 j 位置的所得到的价值。然后就像 Triangle 一样从下往上递推。最终求在0秒的时候,在5位置上的值。

WA了两次,当初求的是0秒的时候,所有位置上的最大值,,这显然是不对的。因为起始位置是5啊。

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 const int MAX = 100000+10;
 8 int a[MAX][11], d[MAX][11];
 9 void init() {
10   int m, i, j, x, t, tMax = -1;
11   while (~scanf("%d", &m) && m) {
12   memset(a, 0, sizeof(a));
13   memset(d, 0, sizeof(d));
14     for (i = 0; i < m; ++i) {
15       scanf("%d%d", &x, &t);
16       if (tMax < t) tMax = t;
17       a[t][x]++;
18     }
19     for (i = 0; i <= 10; ++i) d[tMax][i] = a[tMax][i];
20     for (i = tMax-1; i>= 0; --i) {
21       for (j = 0; j <= 10; ++j) {
22         if (j == 0) {
23           d[i][j] = max(d[i+1][j], d[i+1][j+1]) + a[i][j];
24           continue;
25         } 
26         else if (j == 10) {
27           d[i][j] = max(d[i+1][j], d[i+1][j-1]) + a[i][j];
28           continue;
29         }
30         d[i][j]=max(d[i+1][j], max(d[i+1][j+1], d[i+1][j-1]))+a[i][j];
31       }
32     }
33     printf("%d\n", d[0][5]);
34   }
35 }
36 int main(void) {
37   init();
38   return 0;
39 }

很多题目都是相通的

posted on 2013-05-25 17:51  aries__liu  阅读(214)  评论(1编辑  收藏  举报