HDU 1231 最大连续子序列(动态规划)

来源:互联网 发布:仓库管理java源码下载 编辑:程序博客网 时间:2024/05/01 01:25

题目链接:Click here

思路:这道题是1003的升级版。思路相同

#include <iostream>#include <cstring>#include <cstdio>#include <queue>#include <cstdlib>using namespace std;int a[10005];int main(){    int t, sum, ans, temp, s, e;    while(scanf("%d", &t) != EOF)    {        if(t == 0)break;        temp = 1;        sum = 0;        ans = -10000000;        for(int i = 1; i <= t; i++)        {            scanf("%d", &a[i]);            sum += a[i];            if(ans < sum)            {                ans = sum;                s = temp;                e = i;            }            if(sum < 0)            {                temp = i + 1;                sum = 0;            }          }        if(ans < 0//如果全部为负数,则答案为0,输出整个序列的首尾元素            printf("%d %d %d\n", 0, a[1], a[t]);        else        printf("%d %d %d\n", ans, a[s], a[e]);    }    return 0;}
0 0
原创粉丝点击