【HDU】1003 Max Sum [动态规划]

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

动态规划:f[i]表示以 i 结尾的最大的连续和。则转移方程为

      f[1]=a[1];

     if(f[i-1]+a[i]>=a[i]){

       f[i]=f[i-1]+a[i];

     } else {

         f[i]=a[i];

     }

再顺带更新一下起始终止位置就可以了。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e5+11;
int a[N];
int f[N],st[N],en[N];
int n;

int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        int res=a[1];
        int s=1,e=1;
        f[1]=a[1];
        st[1]=1;en[1]=1;
        for(int i=2;i<=n;i++){
            if(f[i-1]+a[i]>=a[i]){
                f[i]=f[i-1]+a[i];
                st[i]=st[i-1];
                en[i]=i;
                if(res<f[i]){
                    res=f[i];
                    s=st[i];
                    e=en[i];
                }
            } else {
                f[i]=a[i];
                st[i]=i;
                en[i]=i;
                if(res<f[i]){
                    res=f[i];
                    s=st[i];
                    e=en[i];
                }
            }
        }
        printf("Case %d:\n%d %d %d\n",cas,res,s,e);
        if(cas!=T) printf("\n");
    }

    return 0;
}
时间: 2024-08-13 00:27:37

【HDU】1003 Max Sum [动态规划]的相关文章

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(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【动态规划求最大子序列和详解 】

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

HDU acm 1003 Max Sum || 动态规划求最大子序列和详解

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

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

HDU 1003 Max Sum(最大子列和)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 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

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 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> #