http://poj.org/problem?id=2593

 

Maximum Sequence
求数组两段不重叠的连续子数组的最大和
详见2479

 

Description

Give you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N).

【原】 POJ 2593 Max Sequence 动态规划 解题报告

You should output S.

Input

The input will consist of several test cases. For each test case, one integer N (2 <= N <= 100000) is given in the first line. Second line contains N integers. The input is terminated by a single line with N = 0.

Output

For each test of the input, print a line containing S.

Sample Input

5

-5 9 -5 11 20

0

Sample Output

40

 

#include <stdio.h>
#include <iostream>
   3:  
namespace std ;
   5:  
   6:  
int N = 100000 ;
   8:  
int a[N] ;
int maxsofar[2][N] ;
  11:  
void run2593()
  13: {
int n,val ;
int i,j ;
int maxendinghere ;
int max ;
int sum ;
  19:  
, &n ) && n!=0 )
  21:     {
, &a[0] ) ;
  23:         maxendinghere = a[0] ;
  24:         maxsofar[0][0] = a[0] ;
for( i=1 ; i<n ; ++i )
  26:         {
, &a[i] ) ;
  28:             maxendinghere = std::max( a[i] , maxendinghere+a[i] ) ;
  29:             maxsofar[0][i] = std::max( maxsofar[0][i-1] , maxendinghere ) ;
  30:         }
  31:  
  32:         maxendinghere = a[n-1] ;
  33:         maxsofar[1][n-1] = a[n-1] ;
  34:         max = maxsofar[0][n-2] + a[n-1] ;
for( i=n-2 ; i>0 ; --i )
  36:         {
  37:             maxendinghere = std::max( a[i] , a[i]+maxendinghere ) ;
  38:             maxsofar[1][i] = std::max( maxsofar[1][i+1] , maxendinghere ) ;
  39:             sum = maxsofar[0][i-1] + maxsofar[1][i] ;
  40:             max = max>sum ? max : sum ;
  41:         }
 , max ) ;
  43:     }
  44: }

相关文章:

  • 2021-05-30
  • 2021-09-08
  • 2022-03-06
  • 2022-03-07
  • 2022-12-23
  • 2021-06-18
  • 2022-01-29
  • 2021-08-17
猜你喜欢
  • 2021-12-18
  • 2021-11-14
  • 2021-12-12
  • 2022-12-23
  • 2021-12-24
  • 2021-10-02
  • 2021-10-01
相关资源
相似解决方案