Max Sum (hdu 1003 简单DP水过)

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 161294    Accepted Submission(s): 37775

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:  1058 1203 1257 1421 1024

题意:给出n个数的序列,求出最大的子串和,并输出起点和终点。

思路:dp[i]表示以i为结尾的最大子串和。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

int dp[maxn];       //dp[i]表示以i为结尾的最大子串和
int a[maxn];
int n;

int main()
{
    int i,j,t,cas=1;;
    sf(t);
    bool flag=false;
    while (t--)
    {
        if (flag) pf("\n");
        flag=true;
        sf(n);
        FRL(i,1,n+1)
            sf(a[i]);
        mem(dp,0);
        dp[1]=a[1];
        int S=1,T=1,s=1,t=1,maxx=a[1];//s,t记录当前首尾指针,S,T记录当前最大值的首尾指针
        FRL(i,2,n+1)
        {
            if (a[i]>dp[i-1]+a[i])  //如果a[i]对dp[i-1]没有贡献反而会使dp[i-1]减小,那么就以i重新作为起点
            {
                s=i;
                t=i;
                dp[i]=a[i];
            }
            else if (a[i]<=dp[i-1]+a[i])  //a[i]比dp[i-1]+a[i]更大,就将尾指针t向后移赋为i
            {
                t=i;
                dp[i]=dp[i-1]+a[i];     //更dp[i]
            }
            if (dp[i]>maxx)   //如果当前dp[i]比之前的最大值要大,更新最大值,并记录首尾指针
            {
                S=s;
                T=t;
                maxx=dp[i];
            }
        }
        pf("Case %d:\n",cas++);
        pf("%d %d %d\n",maxx,S,T);
    }
    return 0;
}
时间: 2024-08-07 09:07:09

Max Sum (hdu 1003 简单DP水过)的相关文章

[Max Sum]hdu 1003

Water ~ 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 5 using namespace std; 6 const int INF=100000000; 7 8 int t,n,a; 9 int main() 10 { 11 cin>>t; 12 for(int i=1;i<=t;i++){ 13 cin>>n; 14 int sum=0; 15 i

hdu 1024 Max Sum Plus Plus(DP)

转移方程dp[i][j]=Max(dp[i][j-1]+a[j],max(dp[i-1][k] ) + a[j] ) 0<k<j 此链接中有详解点击打开链接 #include<stdio.h> #include<algorithm> #include<iostream> using namespace std; #define MAXN 1000000 #define INF 0x7fffffff int dp[MAXN+10]; int mmax[MAXN

HDU 4826 简单DP

Problem Description 度度熊是一只喜欢探险的熊,一次偶然落进了一个m*n矩阵的迷宫,该迷宫只能从矩阵左上角第一个方格开始走,只有走到右上角的第一个格子才算走出迷宫,每一次只能走一格,且只能向上向下向右走以前没有走过的格子,每一个格子中都有一些金币(或正或负,有可能遇到强盗拦路抢劫,度度熊身上金币可以为负,需要给强盗写欠条),度度熊刚开始时身上金币数为0,问度度熊走出迷宫时候身上最多有多少金币? Input 输入的第一行是一个整数T(T < 200),表示共有T组数据.每组数据的

hdu 1087 简单dp

思路和2391一样的.. <span style="font-size:24px;">#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; const int inf=(0x7f7f7f7f); int main() { int a; int s[10005]; int w[10005];

HDU 1003 Max Sum &amp;&amp; HDU 1231 最大连续子序列 (DP)

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

HDU 1024:Max Sum Plus Plus(DP)

http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24675    Accepted Submission(s): 8478 Problem Description Now I think you have g

HDU OJ Max sum 题目1003

 #include <iostream> #include<stdio.h> #include<stdlib.h> using namespace std; int main() { int t; scanf("%d",&t); for(int i=0;i<t;i++) { int n; scanf("%d",&n); int sum=0,max=-99999; int curhead=1,rear=1,

Hdoj 1024 Max Sum Plus Plus 【DP】

Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18861 Accepted Submission(s): 6205 Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b

HDU1024 Max Sum Plus Plus 【DP】

Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17164    Accepted Submission(s): 5651 Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem