A - Max Sum
Crawling in process...
Crawling failed
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
#include <iostream>
#include<stdio.h>
using namespace std;
int main()
{
int t,a[100010],sum[100010],i,s[100010],ans,kase=1;
scanf("%d",&t);
while(t--)
{
int m,i;
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
sum[0]=a[0];
s[0]=0;
ans=0;
for(i=1;i<m;i++)
{
if(sum[i-1]>=0)
{
sum[i]=sum[i-1]+a[i];
s[i]=s[i-1];
}
else
{
sum[i]=a[i];
s[i]=i;
}
if(sum[ans]<sum[i])
ans=i;
}
printf("Case %d:\n%d %d %d\n",kase++,sum[ans],s[ans]+1,ans+1);
if(t)
printf("\n");
}
return 0;
}
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
本文介绍了一种解决最大子序列和问题的有效算法,并通过示例详细解释了其工作原理。该算法能够从一系列整数中找出具有最大和的连续子序列及其起始和结束位置。
1万+

被折叠的 条评论
为什么被折叠?



