HDOJ 1176 免费馅饼(完全背包)

参考:https://blog.csdn.net/hhu1506010220/article/details/52369785

https://blog.csdn.net/enjoying_science/article/details/38567671

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int N=1e5+10;
 6 int dp[N][12];
 7 int main()
 8 {
 9 //    std::ios::sync_with_stdio(false);//此句用了为wa,望大神解答
10 //    std::cin.tie(0);
11     int n;
12     while (cin>>n,n)
13     {
14         memset(dp,0,sizeof(dp));
15         int s=0,t,x;
16         for (int i=0;i<n;i++)
17         {
18             scanf("%d %d",&x,&t);//注意题目是先输入x再输入t!
19             dp[t][x]++;
20             s=max(s,t);
21         }
22         for (int i=s-1;i>=0;i--)
23         {
24             for (int j=0;j<=10;j++)
25             {
26                 if (j==0)//不能用if..if..if..else..,而要用if..else if..else if..else..!!!因为if只和最近的else匹配
27                 {
28                     dp[i][j]=dp[i][j]+max(dp[i+1][j],dp[i+1][j+1]);
29                 }
30                 else if (j==10)
31                 {
32                     dp[i][j]=dp[i][j]+max(dp[i+1][j-1],dp[i+1][j]);
33                 }
34                 else
35                 {
36                     dp[i][j]=dp[i][j]+max(dp[i+1][j],max(dp[i+1][j-1],dp[i+1][j+1]));
37                 }
38             }
39         }
40         cout<<dp[0][5]<<endl;
41     }
42 
43     return 0;
44 }

 

posted @ 2018-08-08 14:25  hemeiwolong  阅读(186)  评论(0编辑  收藏  举报