码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1003 Max Sum 简单DP

时间:2015-03-01 23:45:12      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

题目链接: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;
}

 

hdu 1003 Max Sum 简单DP

标签:

原文地址:http://www.cnblogs.com/dishu/p/4307749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!