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;
}
时间: 2024-12-18 01:15:14

hdu 1003 Max Sum 简单DP的相关文章

HDU 1003 Max Sum(DP)

题目地址:HDU 1003 DP好弱..先补补DP的基础... 这题就是记录起始点与终止点,然后每当发现一个更大的,就更新.从左往右扫一遍就行了. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #

HDU 1003 Max Sum (dp)

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

hdu 1003 Max Sum 简单动态规划

很简单的动态规划但ac率低于四分之一了,状态转移方程: dp[i]=max(dp[i-1]+a[i],a[i]) 注意几点: case 之间有空格 输入的最小负数为-1000 有多组答案找出第一个的意思是,从头便利,得到第一个最大的和就输出被,然后break: /************************************************************************* > File Name: hdu1231.cpp > Author: yang >

HDU 1003 Max Sum(dp,最大连续子序列和)

Max Sum 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

HDU 1003 Max Sum (动规)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 141547    Accepted Submission(s): 32929 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

HDU 1003 Max Sum 最大连续子序列的和

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

[ACM] hdu 1003 Max Sum(最大子段和模型)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 135262    Accepted Submission(s): 31311 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

HDU - 1003 - Max Sum &amp;&amp; POJ - 1050 - To the Max (经典DP问题)

题目传送:HDU - 1003 思路:最大子序列和 dp[i]= a[i]   (dp[i-1]<0) dp[i]= dp[i-1]+a[i]   (dp[i-1]>=0) AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include

(hdu step 3.2.1)Max Sum(简单dp:求最大子序列和、起点、终点)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1390 Accepted Submission(s): 542   Problem Descrip