zoukankan      html  css  js  c++  java
  • 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;
    }
    所遇皆星河
  • 相关阅读:
    css半透明边框
    css脱离文档流
    margin负值的作用
    浅谈BFC
    css多列布局
    css布局--水平垂直居中
    css布局--垂直居中
    css布局--水平居中
    题解-APIO2019奇怪装置
    题解-AtCoder ARC-078F Mole and Abandoned Mine
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/11568012.html
Copyright © 2011-2022 走看看