POJ 2593 & amp; 2479: Max Sequence

Source: Internet
Author: User

POJ 2593 & 2479: Max Sequence
Max Sequence

Time Limit:3000 MS   Memory Limit:65536 K
Total Submissions:16329   Accepted:6848

Description

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

 

You shoshould 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 200

Sample Output

40

The question is to give a sequence and find the maximum value of the sum of the two subsequences in the sequence. I switched to POJ2479 two or three years ago, but I still didn't understand dp at the time. (Of course, the degree of understanding of dp can also be switched to dp questions ...). So I feel infinite emotion when doing this question. In fact, it is very simple to calculate the sum of a sequence, that is, dp [I] = max (dp [I-1] + value [I], value [I]) now it requires the maximum sum of the two sequences. So I want to come from the left and from the right. Left [I] indicates the maximum sequence and on the left of the number from 1st to the current number I. Right [I] indicates the maximum sequence sum on the right from the number Test (from right to left) to the number I.
Code:
# Include
 
  
# Include
  
   
# Include using namespace std; int left_v [100005]; int right_v [100005]; int value [100005]; int main () {int Test; while (cin> Test) {if (! Test) break; left_v [0] = 0; right_v [0] = 0; left_v [Test + 1] = 0; right_v [Test + 1] = 0; int I, max_v =-100000000; for (I = 1; I <= Test; I ++) {cin> value [I];} left_v [1] = value [1]; right_v [Test] = value [Test]; for (I = 2; I <= Test; I ++) {left_v [I] = max (left_v [I-1] + value [I], value [I]) ;}for (I = Test-1; I> = 1; I --) {right_v [I] = max (right_v [I + 1] + value [I], value [I]);} for (I = 2; I <= Test; I ++) {left_v [I] = max (left_v [I-1], left_v [I]);} for (I = Test-1; i> = 1; I --) {right_v [I] = max (right_v [I + 1], right_v [I]);} for (I = 1; I
   
    
Max_v) max_v = left_v [I] + right_v [I + 1];} cout <
    
     
I am very happy to drop this question. .
      
    
   
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.