hdu 2845——Beans——————【dp】

Beans

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3418    Accepted Submission(s): 1629

Problem Description

Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled with different qualities beans. Meantime, there is only one bean in any 1*1 grid. Now you want to eat the beans and collect the qualities, but everyone must obey by the following rules: if you eat the bean at the coordinate(x, y), you can’t eat the beans anyway at the coordinates listed (if exiting): (x, y-1), (x, y+1), and the both rows whose abscissas are x-1 and x+1.

Now, how much qualities can you eat and then get ?

Input

There are a few cases. In each case, there are two integer M (row number) and N (column number). The next M lines each contain N integers, representing the qualities of the beans. We can make sure that the quality of bean isn‘t beyond 1000, and 1<=M*N<=200000.

Output

For each case, you just output the MAX qualities you can eat and then get.

Sample Input

4 6
11 0 7 5 13 9
78 4 81 6 22 4
1 40 9 34 16 10
11 22 0 33 39 6

Sample Output

242

解题思路:通过两次动态规划,分别对每行和列进行dp。对于每行状态定义:dp[j]表示该行到达第j列时的最大的和。转移方程:dp[j]=max(dp[j-2]+a[j],dp[j-1])。然后用另外的一个d数组记录每行的最大和,相当于矩阵变成了一个列矩阵。再定义状态dq[i]为表示到达该行时的最大和。转移方程:dq[i]=max(dq[i-2]+d[i],dq[i-1])。

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn=211000;
int a[maxn];
int dp[maxn],d[maxn],dq[maxn];
int mmax(int a,int b,int c){
    int ret;
    ret=a>b?a:b;
    return ret>c?ret:c;
}
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        int i,j,k;
        for(i=1;i<=n;i++){
            for(j=1;j<=m;j++){
                scanf("%d",&a[j]);
            }
            dp[0]=0,dp[1]=a[1];
            for(j=2;j<=m;j++){
                dp[j]=max(dp[j-2]+a[j],dp[j-1]);
            }
            d[i]=dp[m];
        }
        dq[1]=d[1];
        for(i=2;i<=n;i++){
            dq[i]=max(dq[i-1],dq[i-2]+d[i]);
        }
        printf("%d\n",dq[n]);
    }
    return 0;
}

  

时间: 2024-08-05 07:03:14

hdu 2845——Beans——————【dp】的相关文章

HDU 2845 Beans (DP)

Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled with different qualities beans. Meantime, there is only one bean in any 1*1 grid. Now you want to eat the beans and collect the qualities, but everyo

hdu 2845 Beans(DP)

题意: M*N的矩阵,每个格子上有一个值. 规则:如果你拾起了某个格子(i,j)上的值,那么第i-1行.第i+1行.(i,j-1)格子上.(i,j+1)格子上的值都不能取. 问最多可以取得多少值(最大值). 思路: 如果某行取了某一个值,则它的前一行和后一行都不能取.所以我们必须知道这行可以取得的最大值是多少. dp[i]=max( dp[i-1],dp[i-2]+a[i] ) dp[i]:前i个数能获得的最大值.第i个可以取,可以不取. 当每行的dp[N]都算出来后,可以发现从行的角度看,dp

hdu 1260 Tickets 【dp】

Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1258    Accepted Submission(s): 599 Problem Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However

HDU 2845 Beans(DP,最大不连续和)

题意    吃豆子游戏    当你吃了一个格子的豆子   该格子左右两个和上下两行就不能吃了    输入每个格子的豆子数    求你最多能吃多少颗豆子 可以先求出每行你最多可以吃多少颗豆子   然后每行就压缩成只有一个格子了   里面的豆子数就是那一行最多可以吃的豆子数   然后问题就变成求一列最多可以吃多少颗豆子了   和处理每一行一样处理   那么问题就简化成求一行数字的最大不连续和问题了 令d[i]表示某一行前i个豆子的最大和  有两种情况  吃第i个格子中的豆子和不吃第i个格子中的豆子

hdu 4908 BestCoder Sequence【DP】

题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=4908 题目大意:给出一个排列,一个m,求出这个排列的连续子序列中有多少个序列式以m为中位数. 由于是一个排列,不会出现重复的数字,记录一下m的位置index,然后以index为分界线,往左求出s[i](表示从i到index之间有多少大于m),b[i](表示从i到index之间有多少小于m),往右求出s[i](表示从index到i之间有多少大于m),b[i](表示从index到i之间有多少小于m).

hdoj 2391 Filthy Rich 【DP】

题目大意:有个二维数组,你从(0,0)出发,最终到(n,m), 在这个二维数组中,每个位置dp[i][j]都有一定量的黄金,你可以拾取,问你最多能失去多少,并且,你的方向有下,右, 斜向下三个方向: 策略:就是每一个都加上它的上方向与左方向的最大值,这样到最后就是最大值.详情见代码 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2391 代码: #include<stdio.h> #include<string.h> int dp[1

HDU 2845 Beans (最大不连续子序列和)

Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2637    Accepted Submission(s): 1302 Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled wi

HDU 2845 Beans (动规)

Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2596    Accepted Submission(s): 1279 Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled w

HDOJ1176 免费馅饼 【DP】+【经典数塔】

免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23986    Accepted Submission(s): 8093 Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的1