Max Sum(hdoj1003)

来源:互联网 发布:mysql不能删除数据 编辑:程序博客网 时间:2024/06/06 05:36

Max Sum

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 255008 Accepted Submission(s): 60595

Problem Description

Given a sequence a[1],a[2],a[3]……a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

Output

For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

Sample Input

2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

Sample Output

Case 1:
14 1 4

Case 2:
7 1 6

Author

Ignatius.L

Recommend

We have carefully selected several similar problems for you: 1176 1069 1058 1159 1203

注释

代码一中的flag我代表的意思是,当前子串的和。
代码二中的flag我代表的意思是,前一个状态的子串和。
我猜你不明白,不明白就对了。

代码一

#include<cstdio>#include<cmath>#include<iostream>#include<string.h>using namespace std;#define MAXN 100001#define MAXM 220#define inf 10e22;int a[MAXN];int main() {    int k;    scanf("%d",&k);    for (int p = 0; p < k; p++)    {        int n;        scanf("%d", &n);        for (int j = 0; j < n; j++)        {            scanf("%d", &a[j]);        }        int max = -100000,flag=0,begin=1,end=1,ansBegin=1,ansEnd=1;        for (int i = 0; i < n; i++)        {            flag = flag + a[i];            if (flag < 0) {                begin = i+1;                end = i+1;            }            else {                end = i + 1;            }            if (flag > max)            {                max = flag;                ansBegin = begin;                ansEnd = end;            }            if (flag < 0)            {                flag = 0;                begin++;            }        //    flag = flag < 0 ? 0 : flag;        }        printf("Case %d:\n",p+1);        printf("%d %d %d\n",max,ansBegin,ansEnd);        if (p!=k-1)        {            printf("\n");        }    }}

代码二

#include<cstdio>#include<cmath>#include<iostream>#include<string.h>using namespace std;#define MAXN 100001#define MAXM 220#define inf 10e22;int a[MAXN];int main() {    int k;    scanf("%d",&k);    for (int p = 0; p < k; p++)    {        int n;        scanf("%d", &n);        for (int j = 0; j < n; j++)        {            scanf("%d", &a[j]);        }        int max = a[0],flag=a[0],begin=1,end=1,ansBegin=1,ansEnd=1;        for (int i = 1; i < n; i++)        {            if (flag >= 0)            {                flag = flag + a[i];                end++;            }            else {                flag = a[i];                begin = i + 1;                end = i + 1;            }            if (flag>max)            {                ansBegin = begin;                ansEnd = end;                max = flag;            }        //    flag = flag < 0 ? 0 : flag;        }        printf("Case %d:\n",p+1);        printf("%d %d %d\n",max,ansBegin,ansEnd);        if (p!=k-1)        {            printf("\n");        }    }}
原创粉丝点击