【HDU】 1176 免费馅饼

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

刚刚在纠结怎么说这个问题的时候,百度了一篇博文,我个人觉得有看了这篇博文,思路也就豁然开朗了,这里附上链接:http://blog.sina.com.cn/s/blog_85ee954b0100wbr3.html

 

数塔的问题我比较喜欢至下而上dp,这是习惯,也是思维方式。

见代码:

View Code
 1 /* *
 2 *hdu 免费馅饼
 3 Time 2011.8.1
 4 * */
 5 #include<iostream>
 6 #include<cmath>
 7 using namespace std; 
 8 int n, tmax, map[11][100010], f[11], nx[11];
 9 
10 int max(int a, int b)
11 {
12     return a>b?a:b;
13 }
14      
15 void deal()
16 {
17      int i, j;
18      for(i=tmax-1; i>=0; --i)
19      {
20          nx[0]=max(f[0],f[1])+map[0][i];
21          for(j=1; j<10; ++j)
22             nx[j]=max(f[j-1],max(f[j],f[j+1]))+map[j][i];
23          nx[10]=max(f[9],f[10])+map[10][i];
24          
25          for(j=0; j<=10; ++j)
26          f[j]=nx[j];
27      }
28      cout<<nx[5]<<endl;
29  }
30        
31 int main()
32 {
33     //freopen("t.in","r",stdin);
34     //freopen("t.out","w",stdout);
35     int i, x, t;
36     while(scanf("%d",&n), n)
37     {
38         memset(map,0,sizeof(map));
39         memset(f,0,sizeof(f));
40         memset(nx,0,sizeof(nx));
41         tmax=0;
42         for(i=0; i<n; ++i)
43         {
44             scanf("%d %d",&x,&t);
45             map[x][t]++;
46             if(tmax<t) tmax=t;
47         }
48         
49         for(i=0; i<=10; ++i)
50         f[i]=map[i][tmax];
51         
52         deal();
53     }
54     return 0;
55 }

 

posted on 2012-08-04 11:06  Yuna_  阅读(69)  评论(0)    收藏  举报