链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1003

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 180817    Accepted Submission(s): 42261

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

5 6 -1 5 4 -7
6 5 10 14 7  
Case 1:
14 1 4

7 0 6 -1 1 -6 7 -5
0 6 5 6 0 7 2
Case 2:
7 1 6

 

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue> using namespace std; #define N 110000
#define INF 0xffffff int sum[N]; int main()
{ int t, iCase=;
scanf("%d", &t); while(t--)
{
int i, n, Max=-INF, Min=INF, a, L, Left, Right; scanf("%d", &n); for(i=; i<=n; i++)
{
scanf("%d", &a);
sum[i] = sum[i-]+a;
}
/// Left = Right = 1;
for(i=; i<=n; i++)
{
///找出最小的sum[i]
if(sum[i-]<Min)
{
Min = sum[i-];
L = i;
}
///找出最大的Max
if(sum[i]-Min>Max)
{
Max = sum[i]-Min;
Left = L;
Right = i;
}
} printf("Case %d:\n", iCase++);
printf("%d %d %d\n", Max, Left, Right); if(t) printf("\n");
}
return ;
}

Max Sum -- hdu -- 1003的更多相关文章

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

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  2. HDU OJ Max sum 题目1003

     #include <iostream> #include<stdio.h> #include<stdlib.h> using namespace std; i ...

  3. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  4. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  5. HDU 1003 Max Sum【动态规划求最大子序列和详解 】

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  6. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  7. hdu 1003 Max Sum (DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)   ...

  8. HDU 1003 Max Sum (动规)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  9. hdu 1003 Max sum(简单DP)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem ...

随机推荐

  1. Makefile编写 三 伪目标的作用

    本节我们讨论一个Makefile中的一个重要的特殊目标:伪目标. 伪目标是这样一个目标:它不代表一个真正的文件名,在执行make时可以指定这个目标来执行其所在规则定义的命令,有时我们也可以将一个伪目标 ...

  2. A Newbie’s Install of Keras & Tensorflow on Windows 10 with R

    This weekend, I decided it was time: I was going to update my Python environment and get Keras and T ...

  3. 在Windows Server 2012启用或关闭Internet Explorer增强的安全配置

    在 Windows Sever 2012 中打开 IE 浏览器时,IE10 会出现[已启用 Internet Explorer 增强的安全配置]的提示信息. 在安全性等级中会设置以[高安全性],如果我 ...

  4. bzoj 3965: [WF2011]Pyramids

    Description 如果你有足够的石块,那么建一座金字塔绝不算难事.举个例子,在一块平地上,我们铺一个10*10的矩形,然后在10*10的矩形上面铺一个9*9的,然后8*8的……以此类推,直到顶上 ...

  5. bzoj 3119: Book

    Description Wayne喜欢看书,更喜欢买书.某天Wayne在当当网上买书,买了很多很多书.Wayne有一个奇怪的癖好,就是第一本书的价格必须恰为X,而之后买的每一本书,若是比上一本更昂贵, ...

  6. MapBuilder,操作集合工具类

    public class MapBuilder { /** * Creates an instance of {@code HashMap} */ public static <K, V> ...

  7. Django ORM-02

    6.ForeignKey 相关操作 1.正向查找 正向查找:那么什么是正向查找,我们知道对于一对多或者多对一的情况,我们一般将ForeignKey设置在多的一边,比如我们的书籍与出版社一般是多对一的, ...

  8. 自定义Hibernate Validator约束

    定义注解 实现一个只能输入指定状态的约束条件 import javax.validation.Constraint; import javax.validation.Payload; import j ...

  9. oracle账户密码更新

    oracle忘记用户名密码怎样恢复 首先cmd - sqlplusw 普通用户登陆:用户名:scott(普通用户名)      密码:tiger(普通用户密码) 管理员登陆:用户名:system 密码 ...

  10. 好记性不如烂笔头-linux学习笔记4apache相关知识

    apache 启动有2种模式 1是prefork模式,每个进程对应一个线程,如果是比较稳定的平台,那么prefork模式是worker模式 比较好,效率高,但是吃的内存比较大. 2 如果是高负载高并发 ...