hdu 1003 Max Sum 简单DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003

 

转移方程:dp[i]=max(dp[i-1]+a[i],a[i])

虽然是dp 但不用真的申请一个dp数组

 

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <set>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

const int maxn = 100100;

int a[maxn];

int main()
{
    //freopen("in.txt", "r", stdin);

    int T;
    scanf("%d", &T);
    for(int t = 1; t <= T; t++)
    {
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            scanf("%d", &a[i]);

        int l, r, tmp_r, tmp_l;
        int ans = -1001;
        int tmp = -1001;


        for(int i = 0; i < n; i++)
        {
            if(tmp + a[i] >= a[i])
            {
                tmp_r = i+1;
                tmp += a[i];
                if(tmp > ans)
                {
                    ans = tmp;
                    l = tmp_l;
                    r = tmp_r;
                }
            }
            else
            {
                tmp_l = i+1;
                tmp_r = i+1;
                tmp = a[i];
                if(tmp > ans)
                {
                    ans = tmp;
                    l = tmp_l;
                    r = tmp_r;
                }
            }
        }


        printf("Case %d:\n", t);
        printf("%d %d %d\n", ans, l, r);
        if(t != T)
            printf("\n");


    }

    return 0;
}

 

posted @ 2015-03-01 22:54  地鼠地鼠  阅读(115)  评论(0编辑  收藏  举报