Nine degrees OJ topic 1372: maximum sub-vectors and (maximum and number of contiguous Sub-arrays)

Source: Internet
Author: User
Tags stdin

Title Description:

Hertz occasionally takes some professional questions to confuse students who are not computer Majors. Today JOBDU Test team after the meeting, he said Again: in the ancient one-dimensional pattern recognition, it is often necessary to calculate the maximum sum of continuous sub-vectors, when the vector is all positive, the problem is well Solved. however, If the vector contains a negative number, should it contain a negative number and expect that the next positive number will compensate for it? For example: {6,-3,-2,7,-15,1,2,2}, the maximum and 8 of continuous sub-vectors (starting from No. 0 to 3rd). Will you be fooled by him?

Input:

Input has multiple sets of data, each set of test data consists of two Rows.

The first behavior is an integer n (0<=n<=100000), and when n=0, the input ends. The next line contains n integers (we guarantee that all integers belong to [ -1000,1000]).

Output:

For each test case, you need to output a single line of 3 integers representing the largest of the successive sub-vectors, the subscript for the first element of the sub-vector, and the subscript for the last Element. If there are multiple sub-vectors, the one with the lowest subscript of the starting element is Output.

Sample Input:
3-1 -3-25-8 3 2 0 586-3-2 7-15 1 2 20
Sample Output:
-1 0 010 1 48 0 3

The first code is as follows
1#include <cstdio>2#include <cstdlib>3#include <iostream>4#include <algorithm>5  6 using namespacestd;7 intn;8 intnum[100002];9  Ten intMainintargcChar Const*Argv[]) one { a     //freopen ("input.txt", "r", stdin); -      while(SCANF ("%d", &n)! = EOF && n! =0) { -          for(inti =0; I < n; i++) { thescanf"%d",&num[i]); -         } -         intMaxx =-1002, cnt =0; -         ints =0, state =0; +         intSt =0, et =0; -          for(inti =0; I < n; i++) { +             if(state = =0) { as =i; atState =1; -             } -CNT = cnt +num[i]; -             if(cnt >maxx) { -Maxx =cnt; -St =s; inET =i; -             } to             if(cnt <0) { +CNT =0; -State =0; the             } *         } $printf"%d%d%d\n", maxx,st,et);Panax Notoginseng     }    -     return 0; the}

Intermediate judgment is a bit complicated, refer to others, modify the following

1#include <cstdio>2#include <cstdlib>3#include <iostream>4#include <algorithm>5 6 using namespacestd;7 intn;8 intnum[100002];9 Ten intMainintargcChar Const*Argv[]) one { a     //freopen ("input.txt", "r", stdin); -      while(SCANF ("%d", &n)! = EOF && n! =0) { -         intMaxx =-1002, cnt =0; the         ints =0; -         intSt =0, et =0; -  -          for(inti =0; I < n; i++) { +scanf"%d",&num[i]); -  +             if(cnt >=0) { aCNT = cnt +num[i]; at             } -             Else { -CNT =num[i];  -s =i; -             } -             if(cnt >maxx) { inMaxx =cnt; -St =s; toET =i; +             } -         } the          *printf"%d%d%d\n", maxx,st,et); $     }    Panax Notoginseng     return 0; -}

Nine degrees OJ topic 1372: maximum sub-vectors and (maximum and number of contiguous Sub-arrays)

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.