Now, how much qualities can you eat and then get ?
InputThere 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.OutputFor 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 6Sample Output
242
思路:求最大不连续和
#include <iostream> #include <iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include <stdio.h> #include <string.h> #include <vector> #include <set> using namespace std; int dpm[2009][2009] , dpn[2009] ;//dpm求每行的不连续序列和,dpn求每一列的不连续序列和 int len1 , len2 ; int main() { int n , m ; while(cin >> n >> m) { memset(dpn , 0 , sizeof(dpn)); memset(dpm , 0 , sizeof(dpm)); for(int i = 3 ; i < n + 3 ; i++) { for(int j = 3 ; j < m + 3 ; j++) { scanf("%d" , &dpm[i][j]); dpm[i][j] += max(dpm[i][j-2] , dpm[i][j-3]);//求dp[m+1] 和 dp[m+2]代表两种选择方式最大行不连续和 } } for(int i = 3 ; i < n + 3 ; i++) { dpn[i] = max(dpn[i-2] , dpn[i-3]) + max(dpm[i][m+1] , dpm[i][m+2]);//求不连续最大列序列和 } int ans = 0 ; for(int i = 3 ; i < n + 3 ; i++) { if(dpn[i] > ans) ans = dpn[i]; } printf("%d\n" , ans); } return 0; }