Nine degrees OJ topic 1527: Maximum subarray of the connected array and

Source: Internet
Author: User

Title Description:

Given an array of n integer elements arr, there are positive and negative numbers in the array, this array is not a normal array, and its end-to-end is connected. One or more contiguous elements in an array can form a subarray, where such a subarray exists arr[i],... arr[n-1],arr[0],..., arr[j], now please this acm_ Lover uses one of the most efficient methods to help find out the maximum value of all successive sub-arrays (if the elements in the array are all negative, the maximum is 0, or none is selected).

Input:

The input contains multiple test cases, each with two rows, the first line being an integer n (1=<n<=100000), the length of the array, and the second line entering n integers (the absolute value is not greater than 1000).

Output:

For each test case, output the maximum value of the sub-array and.

Sample input:
61-2 3 5-1 256-1 5 4-7
Sample output:
1014
This problem is very valuable to do, at first glance not difficult, but do not know how to do it.
The idea at first is to count each negative as the beginning, and time out.
Later thought of the topic is actually to find an opening point, so that the beginning point to calculate the sub-array and the smallest,
So, is this starting point the smallest negative number in the array?
Press this code, submit, Answer wrong

To cite a counter-example
6
-7 8-6 2-6 8

-7 is the smallest, but -6+2-6 is smaller than-7
So we should first find the smallest and sub-arrays in the array, but doesn't that turn out to be the original problem? (The answer can be obtained from any one of the array to begin the request.)
At this point, we can consider this problem, if the minimum and the array does not involve the problem of loops, then you can find the general method, and then at the end of the arrays at the beginning of the next to find the most large
If this minimum and array exists in the end-to-end position, then we can find the answer from the beginning to the maximum and (that is, from the middle of the array).
The code is as follows
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5 using namespacestd;6 intnum0[100002];7 intnum[100002];8  9 intN;Ten   One intFindintstart) { A     intMaxi =0; -     intCNT =0; -      for(inti = start; I < n; i++) { the         if(CNT <0) { -CNT =Num[i]; -         } -         Else { +CNT = cnt +Num[i]; -         } +         if(CNT >Maxi) { AMaxi =CNT; at         } -     } -      for(inti =0; I < start; i++) { -         if(CNT <0) { -CNT =Num[i]; -         } in         Else { -CNT = cnt +Num[i]; to         } +         if(CNT >Maxi) { -Maxi =CNT; the         } *     } $     returnMaxi;Panax Notoginseng } - intMainintargcChar Const*argv[]) the { +      while(SCANF ("%d", &n)! =EOF) { A         intj =0; thememset (NUM,0,sizeof(num)); +         intState =0; -          for(inti =0; I < n; i++) { $scanf"%d",&num0[i]); $             if(Num0[i] >0) { -                 if(state = =1) { -J + +; theState =0; -                 }WuyiNum[j++] =Num0[i]; the                   -             } Wu             Else { -NUM[J] = Num[j] +Num0[i]; About                 if(state = =0) { $State =1; -                 } -                   -             } A         } +n = j +State ; the   -         /*for (int i = 0; i < n; i++) { $ printf ("%d", Num[i]); the         } the puts ("");*/ the         intLocal =1002,Global=1002; the         intMini =-1; -         intSumi =0; in          for(inti =0; I < n; i++) { theLocal = min (local+Num[i], num[i]); the             if(Local <Global) { AboutMini =i; the                 Global=Local; the             } the         } +         intMaxa = Find (0); -         intMaxt =find (mini); the         if(Maxa <maxt) {BayiMaxa =Maxt; the         } theprintf"%d\n", Maxa); -     }    -     return 0; the}

Nine degrees OJ topic 1527: Maximum subarray of the connected array and

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.