HDU-1231 简单dp,连续子序列最大和,水

1、HDU-1231   

2、链接:http://acm.hdu.edu.cn/showproblem.php?pid=1231  

3、总结:水

题意:连续子序列最大和

#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstdio>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f

int main()
{
    int k,dp[10010],a[10010];
    while(scanf("%d",&k)!=EOF,k)
    {
        int maxn=-INF,left,right,flag;
        dp[0]=0;
        for(int i=1;i<=k;i++)
        {
            scanf("%d",&a[i]);
            if(dp[i-1]>0){
                dp[i]=dp[i-1]+a[i];
            }
            else {
                dp[i]=a[i];
                flag=i;
            }

            if(maxn<dp[i]){
                left=flag;
                right=i;
                maxn=dp[i];
            }

        }

        if(maxn<0){
            maxn=0,left=1,right=k;
        }
        printf("%d %d %d
",maxn,a[left],a[right]);
    }

    return 0;
}
View Code
原文地址:https://www.cnblogs.com/sbfhy/p/5761201.html