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
题意:仍旧是最大连续子序列和;
注意输出格式就行;
AC代码:
#include <bits/stdc++.h>
#define ll long long
using namespace std ;
int main()
{
int l , r , maxi , temp , now ;
int p1, p2 ;
int t ;
cin>> t ;
for(int cas = 1 ; cas <=t ; cas++)
{
int n ;
cin>>n>>temp ;
now=maxi=temp;
r=l=p1=p2=1;
for(int i=2; i<=n; i++)
{
cin>>temp;
if(temp>now+temp)
{
now=temp;
p1=i;
}
else
{
now+=temp;
}
if(now>maxi)
{
l=p1;
r=i;
maxi=now;
}
}
printf("Case %d:\n",cas);
printf("%d %d %d\n",maxi , l , r);
if(cas!=t)
printf("\n");
}
return 0 ;
}
版权声明:本文为qq_33638791原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。