您的位置:首页 > 编程语言

2016SDAU编程练习三1001

2016-05-15 21:29 393 查看
Problem A

Problem Description

 

Given a sequence a[1],a[2],a[3]......a
, 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.<br>

 

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).<br>

 

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.<br>

 

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<iostream>

#include<string.h>

#include<set>

#include<stdio.h>

#include<vector>

#include<algorithm>

#include<numeric>

#include<math.h>

#include<string.h>

#include<sstream>

#include<stdio.h>

#include<string>

#include<cstdlib>

#include<algorithm>

#include<iostream>

#include<map>

#include<queue>

#include<iomanip>

#include<cstdio>

using namespace std;

int main()

{

    //freopen("r.txt","r",stdin);

    int n,m,sum,maxx,j,k;

    int a[100005];

    cin>>n;

    int ans=1,step;

    while(n--)

    {

        cin>>m;

        for(int i=0;i<m;i++)

        {

            scanf("%d",&a[i]);

        }

        sum=0;

        maxx=-1001;

        step=1;

        for(int i=0;i<m;i++)

        {

            sum+=a[i];

                if(sum>maxx)

                    {

                        j=step;

                        k=i+1;

                        maxx=sum;

                    }

            if(sum<0)

            {

                sum=0;

                step=i+2;

            }

        }

        cout<<"Case "<<ans++<<":"<<endl;

        cout<<maxx<<" "<<j<<" "<<k<<endl;

        if(n>0) cout<<endl;

    }

    return 0;

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: