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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2845

思路:对于一行来说,相邻的数不可同时取,容易得到状态转移方程:sum[i] = max (sum[i-2]+sum[i], sum[i-1]),其中sum[i]表示一行前i个数时的最大和;然后把sum[m]保存到另一个数组中,对于每一行都这么做,然后最后在对数组再次进行一边这样的操作就行了;

View Code
 1 #include<iostream>
 2 const int N=200020;
 3 using namespace std;
 4 
 5 int a[N],b[N];
 6 
 7 int main(){
 8     int n,m;
 9     while(~scanf("%d%d",&n,&m)){
10         for(int i=1;i<=n;i++){
11             for(int j=1;j<=m;j++){
12                 scanf("%d",&a[j]);
13             }
14             for(int j=2;j<=m;j++){
15                 a[j]=max(a[j-2]+a[j],a[j-1]);
16             }
17             b[i]=a[m];
18         }
19         for(int i=2;i<=n;i++){
20             b[i]=max(b[i-2]+b[i],b[i-1]);
21         }
22         printf("%d\n",b[n]);
23     }
24     return 0;
25 }
原文地址:https://www.cnblogs.com/wally/p/2956523.html