Max Sum (最大连续子序列)

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


#include
using namespace std;
#define MT(a,b) memset(a,b,sizeof(a))
#define INF INT_MAX
#define ONF INT_MIN
#define ll long long
const int maxn = 1e5+5;
int main()
{
    int m;cin>>m;
    int l=0;
    while(m--)
    {
        int n;cin>>n;
        pairf[maxn];MT(f,0);//pair f[i]表示起点(second)到i的和(first),意为第i个数为尾数的最大连续子序列和
        ll a[maxn];MT(a,0);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            if(f[i-1].first<0||i==1)//上一个起点到到i-1点的和f[i-1]为负数时另起一个起点
            {
                f[i].first=a[i];
                f[i].second=i;//记录起点位置
            }
            else//若f[i-1]为正数,f[i]就合并f[i-1]
            {
                f[i].first=f[i-1].first+a[i];
                f[i].second=f[i-1].second;//记录f[i]的起点
            }
        }
        ll ansSum=ONF,ansLeft=0,ansRight=0;
         //查找最大值
        for(int i=1;i<=n;i++)
        {
            if(f[i].first>ansSum)
            {
                ansSum=f[i].first;
                ansLeft=f[i].second;
                ansRight=i;
            }
        }
        cout<<"Case "<<++l<<":"<

你可能感兴趣的:(Max Sum (最大连续子序列))