HDU1003——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 contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

Output

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

Sample Input

2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

Sample Output

Case 1:
14 1 4

Case 2:
7 1 6

Author

Ignatius.L

Recommend

We have carefully selected several similar problems for you:  1176 1087 1069 2084 1058

很简单的dp,dp[i]表示以i为开头的值

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
int a[maxn],dp[maxn];
int T,n;

int main()
{
    scanf("%d",&T);
    for(int cas = 1; cas <= T; cas++){
        scanf("%d",&n);
        for(int i = 1; i <= n ;i++){
            scanf("%d",&a[i]);
            dp[i] = a[i];
        }
        for(int i = n-1; i >= 1; i--){
            dp[i] = max(dp[i], dp[i+1] + a[i]);
        }
       // for(int i = 1; i <= n; i++)
       //     printf("%d ",dp[i]);
        int index;
        int max1 = -inf;
        for(int i = 1; i <= n ;i++){
            if(max1 < dp[i]){
                 max1 = dp[i];
                index = i;
            }
        }
        int res = 0;
        int index1;
        for(int i = index; i <= n; i++){
            res += a[i];
            if(res == max1){
                index1 = i;
                break;
            }
        }
    printf("Case %d:\n",cas);
    printf("%d %d %d\n",max1,index,index1);
   if(cas < T) printf("\n");
    }
    return 0;
}

  

时间: 2024-10-17 22:39:54

HDU1003——DP——Max Sum的相关文章

hdu1003 1024 Max Sum&amp;Max Sum Plus Plus【基础dp】

dp是竞赛中常见的问题,也是我的弱项orz,更要多加练习.看到邝巨巨的dp专题练习第一道是Max Sum Plus Plus,所以我顺便把之前做过的hdu1003 Max Sum拿出来又做了一遍 HDU 1003 Max Sum 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目描述:给一个整数序列,求其中的一段连续子序列,使它的和最大值以及这个序列的起始下标 思路:简单的dp,抓住“连续”来入手.构造pair<int,int> dp[

HDU1024——DP——Max Sum Plus Plus

Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem. Given a consecutive number sequ

[VJ][DP]Max Sum Plus Plus

Max Sum Plus Plus Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem. Given a consecutive n

HDU-1003:Max Sum(优化)

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

HDU 1024 DP Max Sum Plus Plus

题意:本题的大致意思为给定一个数组,求其分成m个不相交子段和最大值的问题. kuangbin专题. dp[i][j]=Max(dp[i][j-1]+a[j] , max( dp[i-1][k] ) + a[j] ) 0<k<j 注意可以换成一维数组.第一维i没有影响,我们要求dp[m][n],所以i维可以省去,每次保留前j-1个数之中最大的部分. 1 #include<iostream> 2 #include<string> 3 #include<algorith

hdu1003 Max Sum(经典dp )

A - 最大子段和 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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 thi

ACM学习历程—HDU1003 Max Sum(dp &amp;&amp; 最大子序列和)

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 an inte

HDU1003 Max Sum

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题意:给你一组数字,求出最大的字段和. 思路:这是一个经典的dp题目,定义数组a储存一组数字,a[j]为ji个数,dp[j]表示已j结尾的最大字段和,那么dp[j]=max(dp[j-1]+a[j],dp[j]). 例如: a[]       6   -1   5    4    -7 dp[]     6    5   10  14    7 代码如下: #include <iostream

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面