The sword refers to the largest subvector of OFFER and (the largest sum of consecutive subarrays) (9 degrees OJ1372)

Source: Internet
Author: User

Description:

HZ occasionally fooled non-computer students with some professional questions. Today, after the JOBDU test group was opened, he spoke again: in the Ancient One-dimensional pattern recognition, it is often necessary to calculate the largest sum of continuous subvectors. When all vectors are positive, the problem is well solved. However, if a vector contains a negative number, should it contain a negative number and expect the positive number next to it to compensate? For example: {6,-3,-0th,-15, 1, 3rd}, the maximum sum of continuous subvectors is 8 (starting from ). Will you be fooled by him?

 

Input:

The input contains multiple groups of data. Each group of test data includes two rows.

The first act is an integer n (0 <= n <= 100000). When n = 0, the input ends. The next row contains n integers (we guarantee that all integers belong to [-]).

 

 

 

 

Output:

For each test case, three integers must be output in a single row, representing the largest continuous subvector and the subscript of the first element of the subvector and the subscript of the last element respectively. If multiple subvectors exist, the smallest 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
Solution:

Algorithm logic:

WeRecord the largest sum of the current scan and the largest sum of the records. If the current sum exceeds the largest sum, it is replaced and the coordinates at both ends are recorded. Otherwise, the scan continues.

while(i<n){            if(newMax < 0){                newMax = gArr[i];                newBegin = i;                newEnd = i;            }else{                newMax += gArr[i];                newEnd = i;            }            if(newMax > realMax){                realMax = newMax;                realBegin = newBegin;                realEnd = newEnd;                flag = 1;            }            i++;        }

 

However, the test case for this question is strange.Only the largest forward child segment is supported, not the largest backward field.. For example:

WeInput-1 0 5 0 0 should get 5 1 2 instead of 5 1 4, that is, it satisfies the maximum child segment forward, but does not guarantee to satisfy the backward, however, it may not be such a case, and my code is just stepping on the block area.At least my use cases prove this. If the problem persists, correct the problem. The second test case is strange.

All code:
#include <stdio.h>#include <stdlib.h>#define MAXSIZE 100000int gArr[MAXSIZE] = {0};int main(){    int n,i;    int realMax,realBegin,realEnd;    int newMax,newBegin,newEnd;    int flag;    while(scanf("%d",&n)!=EOF && n>0 && n <= 100000){        for(i=0;i<n;i++){            scanf("%d",&gArr[i]);        }        realMax=-1001,realBegin=0,realEnd=0;        newMax=-1001,newBegin=0,newEnd=0;        flag = 0;        i=0;        while(i<n){            if(newMax < 0){                newMax = gArr[i];                newBegin = i;                newEnd = i;            }else{                newMax += gArr[i];                newEnd = i;            }            if(newMax > realMax){                realMax = newMax;                realBegin = newBegin;                realEnd = newEnd;                flag = 1;            }            i++;        }        if(flag)            printf("%d %d %d\n",realMax,realBegin,realEnd);        else            printf("%d %d %d\n",-1,0,0);    }    return 0;}/**************************************************************    Problem: 1372    User: xhalo    Language: C    Result: Accepted    Time:450 ms    Memory:1304 kb****************************************************************/

 

Related Article

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.