Beans
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4456 Accepted Submission(s): 2105
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 ?
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
题目的意思是在一个矩阵中取数字,取数规则是如果取了一个数a[i][j],那么他的上一行和下一行和前后两个数都不能取了。
处理时先在每一行算出最大不连续子序列的和,保存到一个数组里,当所有行都处理完之后,我们发现在列上也是一个球最大不连续子序列和的问题
由于数据较大,数组开不下,我们采用输一行处理一行
对于每个数dp[i]如果取它的话最大值一定是他到前面2个或3个的最大值加上他自己,即dp[i]=max(dp[i-2],dp[i-3])+a[i];因为i-1不能取,而i-4升至更大的话,中间空了一个可以取得数。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f int dp[200005];
int b[200005];
int a[200005];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
scanf("%d",&a[j]);
} dp[0]=a[0];
dp[1]=a[1];
dp[2]=dp[0]+a[2];
for(int j=3;j<m;j++)
{
dp[j]=max(dp[j-2],dp[j-3])+a[j];
}
b[i]=max(dp[m-1],dp[m-2]);
}
dp[0]=b[0];
dp[1]=b[1];
dp[2]=dp[0]+b[2];
for(int j=3;j<n;j++)
{
dp[j]=max(dp[j-2],dp[j-3])+b[j];
}
int ans=max(dp[n-1],dp[n-2]);
printf("%d\n",ans); } return 0;
}