# HDU 1003 Max Sum

213人阅读 评论(0)

#include <iostream>
#include <cstdio>
#define maxn 100010
using namespace std;
int T,N,a[maxn];

int main()
{
//freopen("in.txt","r",stdin);
int g=1,flag;
scanf("%d",&T);
while(T--)
{
int begin,end,t;
__int64 sum,maxsum;
begin=end=0;
scanf("%d",&N);
for(int i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
sum=maxsum=a[0],flag=0,t=0;
for(int i=1;i<N;i++)
{
if(sum<0)
{
sum=a[i];
if(sum>maxsum)
{
maxsum=sum;
end=i;
}
t=0;
continue;
}
if(sum+a[i]<a[i])
sum=a[i];
else
{
sum+=a[i];
t++;
}
if(sum>maxsum)
{
maxsum=sum;
end=i;
flag=t;
}
}
begin=end-flag;
printf("Case %d:\n%I64d %d %d\n",g,maxsum,begin+1,end+1);
g++;
if(T)
printf("\n");
}
return 0;
}


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.

2
5 -3 -2 -1 -2 -3 ———— -1 3 3
-1 -2 -3 -4 -5 -6—————-1 1 1
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：583次
• 积分：26
• 等级：
• 排名：千里之外
• 原创：2篇
• 转载：0篇
• 译文：0篇
• 评论：0条
文章分类
文章存档
阅读排行
评论排行