Max Sum (动态规划)

时间:2019-09-22
本文章向大家介绍Max Sum (动态规划),主要包括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-154-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

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

Sample Output

copy
Case 1: 14 1 4 Case 2: 7 1 6


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#define lli long long
#define mem(s,t) memset(s,t,sizeof(s))
#define ok return 0;
#define rep(x) for(int i=0;i<x;i++) cin>>a[i];
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int a[100000+5];
int main()
{
    TLE;
    int t,n,m,k,l,r,ll,rr,en;
    lli mx,cnt,ans,now;
    cin>>t;
    for(int tt=1; tt<=t; tt++)
    {
        cin>>n;
        rep(n);
        l=r=cnt=ans=ll=rr=en=0;
        mx=-10000000000;
        for(int i=0; i<n; i++)
        {
            if(cnt>=0)
            {
                now = cnt+a[i];
                r = i;
            }
            else
            {
                now = a[i];
                l = r = i;
            }
            if(now >= mx)
            {
                mx = now;
                ll = l ;
                rr = r;
            }
            cnt = now;
        }
        //cout<<mx<<endl;
        cout<<"Case "<<tt<<":"<<endl;
        cout<<mx<<" "<<ll+1<<" "<<rr+1<<endl;;
        if(tt!=t)  cout<<endl;
    }
    ok;
}

原文地址:https://www.cnblogs.com/Shallow-dream/p/11568012.html