链接:

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. volatile的本质

    1. 编译器的优化 在本次线程内, 当读取一个变量时,为提高存取速度,编译器优化时有时会先把变量读取到一个寄存器中:以后,再取变量值时,就直接从寄存器中取值:当变量值在本线程里改变时,会同时把变量的新 ...

  2. 简单桶排序算法-python实现

    #-*- coding: UTF-8 -*- import numpy as np def BucketSort(a, n): barrel = np.zeros((1, n), dtype = 'i ...

  3. CentOS7.2 安装redis 3.0.6集群

    1.环境确认 a.系统版本查看 [hadoop@p168 ~]$ cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core)  b.安装依 ...

  4. 学习blus老师js(6)--js运动基础

    运动基础 一.匀速运动 运动框架 在开始运动时,关闭已有定时器 把运动和停止隔开(if/else) <!DOCTYPE HTML> <html> <head> &l ...

  5. mysql connection phase(未整理)

    14.2.1 初始握手初始握手从服务器发送 Initial_Handshake_Packet开始.在这之后,客户端可以选择是否通过SSL_Connection_Request_Packet发送SSL连 ...

  6. 详解Oracle的几种分页查询语句

    转载自:http://database.51cto.com/art/200904/118737.htm 分页查询格式: SELECT * FROM (SELECT A.*, ROWNUM RN FRO ...

  7. servlet中获取配置文件中的参数.

    web.xml (添加init-param) <?xml version="1.0" encoding="UTF-8"?> <web-app ...

  8. 2017 年 PHP 程序员未来路在何方?

    PHP 从诞生到现在已经有20多年历史,从Web时代兴起到移动互联网退潮,互联网领域各种编程语言和技术层出不穷, Node.js . GO . Python 不断地在挑战 PHP 的地位.这些技术的推 ...

  9. servlet的小例子

    servlet测试 首先,打开myeclipse,file|new|Dynamic Web Project 会出现一个对话框,在Project name的文本框中输入:FirstServlet:然后点 ...

  10. React基本实例

    学习React不是一蹴而就的事情,入门似乎也没那么简单.但一切都是值得的. 今天给大家带来一个详细的React的实例,实例并不难,但对于初学者而言,足够认清React的思考和编写过程.认真完成这个实例 ...