HDU 1003:Max Sum(DP)

Max Sum

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

Total Submission(s): 142742    Accepted Submission(s): 33225

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

这里再给出一些测试数据:

4 0 0 2 0 —— 2 1 3
6 2 7 -9 5 4 3 —— 12 1 6
4 0 0 -1 0 —— 0 1 1
7 -1 -2 -3 -2 -5 -1 -2 —— -1 1 1
6 -1 -2 -3 1 2 3 —— 6 4 6
5 -3 -2 -1 -2 -3 —— -1 3 3

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<cmath>

using namespace std;
const int maxn = 100000 + 150;

int t;
int n;
int temp;
int start;
int last;
int a[maxn];
int ans;
int sum;

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]);
        sum = a[1];
        ans = a[1];//开始将其赋值为0 。。WA。。
        temp = 1;
        start = 1;
        last = 1;//以为last不用初始也可以。。WA。。
        for(int j=2; j<=n; j++)
        {
            if( sum<0 )
            {
                temp = j;
                sum = 0;
            }
            sum = sum + a[j];
            if( sum>ans )
            {
                ans = sum;
                last = j;
                start = temp;
            }
        }
        printf("Case %d:\n", cas);
        printf("%d %d %d\n", ans, start, last);
        if( cas<t )
            printf("\n");
    }

    return 0;
}

HDU 1003:Max Sum(DP)

时间: 2024-12-22 12:48:55

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

Max Sum (dp)

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. InputThe first line of the input contains an integer T(1<=T<=

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 4825:Xor Sum(Trie)

http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意:给出N个数,M个询问,每个询问给出一个X,问在这N个数中哪个数和X异或后结果最大. 思路:可以用Trie构造出sigmaSize为0和1的点,先将N个数插入Trie,然后询问在Trie上尽量找可以不同的数,不同的话异或起来是最大的. 1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 #in

hdu 5623 KK&#39;s Number(dp)

问题描述 我们可爱的KK有一个有趣的数学游戏:这个游戏需要两个人,有N\left(1\leq N\leq 5*{10}^{4} \right)N(1≤N≤5∗10?4??)个数,每次KK都会先拿数.每次可以拿任意多个数,直到NN个数被拿完.每次获得的得分为取的数中的最小值,KK和对手的策略都是尽可能使得自己的得分减去对手的得分更大.在这样的情况下,最终KK的得分减去对手的得分会是多少? 输入描述 第一行一个数T\left( 1\leq T\leq 10\right)T(1≤T≤10),表示数据组

HDU 4968 Improving the GPA(dp)

HDU 4968 Improving the GPA 题目链接 dp,最大最小分别dp一次,dp[i][j]表示第i个人,还有j分的情况,分数可以减掉60最为状态 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int t, avg, n; double dp1[15][405], dp2[15][405]; double get(int x) { if

2014多校第七场1005 || HDU 4939 Stupid Tower Defense (DP)

题目链接 题意 :长度n单位,从头走到尾,经过每个单位长度需要花费t秒,有三种塔: 红塔 :经过该塔所在单位时,每秒会受到x点伤害. 绿塔 : 经过该塔所在单位之后的每个单位长度时每秒都会经受y点伤害. 蓝塔 : 经过该塔所在单位之后,再走每个单位长度的时候时间会变成t+z. 思路 : 官方题解 : 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #define LL long long

HDU 5617 Jam&#39;s maze(DP)

题目链接:点击打开链接 题意:给你一个n*n的矩阵.  求从(1,1)走到(n,n)所组成的回文串个数. 思路:一开始傻逼把状态写成了d[x][y][s],s表示一个串, 用map存的, 后来发现极不可行, 因为这个状态简直太大了, 包括了s串的所有情况. 只是相当于一个dfs中的剪枝罢了. 后来想到, 其实串是不必记录的, 我们只要统计个数, 所以不妨在DP的过程中就判断回文串的情况, 那么就需要同时记录两头的情况.  为了不爆内存, 将状态表示成d[i][x1][x2], 表示走了i步, 左