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

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
 
题意:在图中取数,例如取了81之后,同一行的相邻两个不能取,还有81的上面那行和下面那行也不能取,问能取到的最大和是多少?
思路:分别求行列的最大不连续子序列和
#include <cstdio>
#include <map>
#include <iostream>
#include<cstring>
#include<bits/stdc++.h>
#define ll long long int
#define M 6
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
int sum[20007][2];
int main(){
    ios::sync_with_stdio(false);
    int m,n;
    while(cin>>m>>n){
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                cin>>sum[j][0];
            }
            for(int j=2;j<=n;j++)
                sum[j][0]=max(sum[j][0]+sum[j-2][0],sum[j-1][0]);
            sum[i][1]=sum[n][0];
        }
        for(int i=2;i<=m;i++)
            sum[i][1]=max(sum[i][1]+sum[i-2][1],sum[i-1][1]);
        cout<<sum[m][1]<<endl;    
    }
}

猜你喜欢

转载自www.cnblogs.com/wmj6/p/10391080.html