现在的位置: 首页 > 综合 > 正文

HDU 1003 Max Sum 最大连续上升和

2018年01月20日 ⁄ 综合 ⁄ 共 1343字 ⁄ 字号 评论关闭

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 139206    Accepted Submission(s): 32328

Problem 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
/*
HDU 1003 最长上升序列和 
用len1 len2表示起始结束的位置 
*/

#include <iostream>
using namespace std;
int a[100001];
int main()
{
    int n,m,i,max,sum,len1,len2,k,j;
    cin>>m;
    k=1;
    while (m--)
    {
        scanf("%d",&n);
        max=-1001;
        sum=0;
        len1=1;
        len2=1;
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            if(sum<0) 
                sum=a[i];//正的就往上加
            else sum+=a[i];
            
            if(sum>max)
                {
                    max=sum; 
                    len2=i+1;    
                }
        }
        
        for(j=0;j<n;j++)    
        {
            sum=0;
            for(i=j;i<len2;i++)
                sum+=a[i];
            if(sum==max)
                {
                    len1=j+1;
                    break;
                }
        }          
        printf("Case %d:\n",k++);
        printf("%d %d %d\n",max,len1,len2);
        if(m!=0) printf("\n");
    }
    return 0;
}

抱歉!评论已关闭.