Max Sum

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

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

 
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int num[100086];
int main()
{
int T;
scanf("%d",&T);
int cases=0;
while(T--){
cases++;
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&num[i]);
}
int l=1,r=1;
int x,y;
int maxx=-999999999,sum=0;
for(int i=1;i<=n;i++){
sum+=num[i];
if(sum>maxx){
x=l;y=i;
maxx=sum;
}
if(sum<0){sum=0;l=i+1;}
}
if(cases!=1){printf("\n");}
printf("Case %d:\n",cases);
printf("%d %d %d\n",maxx,x,y);
}
}

  

HDU 1003 Max Sum 求区间最大值 (尺取法)的更多相关文章

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

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

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

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

  3. hdu 1003 Max Sum (DP)

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

  4. HDU 1003 Max Sum * 最长递增子序列(求序列累加最大值)

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

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

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

  6. HDU 1003 Max Sum (动态规划 最大区间和)

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

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

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

  8. HDU 1003 Max Sum 解题报告

    题目大意:求一串数字中,几个连续数字加起来最大值,并确定起始和最末的位置. 思路:这是一题DP题,但是可以用尺取法来做.我一开始不会,也是看了某大神的代码,然后有人告诉我这是尺取法,现在会了. //尺 ...

  9. 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 ...

随机推荐

  1. timestamp 与 nonce 防止重放攻击

    重放攻击是计算机世界黑客常用的攻击方式之一,所谓重放攻击就是攻击者发送一个目的主机已接收过的包,来达到欺骗系统的目的,主要用于身份认证过程. 首先要明确一个事情,重放攻击是二次请求,黑客通过抓包获取到 ...

  2. Delphi (Library Path Browsing Path)

    首先要明白的一个概念是dcu文件 *.dcu是*.pas的编译后单元文件(Delphi Compiled Unit), 编译器把它和库文件连接起来就构成了可执行文件*.exe 或*.dll等,相当于C ...

  3. delphi 中出现dataset not in edit or insert mode的问题

    self.ADOQuery2.Edit;self.ADOQuery2.First;while not self.ADOQuery2.Eof dobeginself.ADOQuery2.FieldByN ...

  4. File类 详解!

    package cn.zhoucufeng; import java.io.File; import java.io.IOException; import java.util.Arrays; imp ...

  5. LODOP打印控件进行批量打印

    Lodop打印控件批量打印的方式:1.批量打印每页内容相同的:(1)批量打印相同内容的很多纸张,可以设置打印份数,把该内容打印出多份.2.批量打印每页不同内容的:(1)通过在一个任务中分页,循环添加页 ...

  6. Python学习之路————Day03

    今日内容: 1.IDE开发环境的安装 2.基本数据类型的使用 3.运算符 一. 算术运算符: 比较运算符 赋值运算符 逻辑运算符 身份运算符:is比较的是id 而==比较的是值 二.基本数据类型 为什 ...

  7. UVA 1602 Lattice Animals

    题目 输入n.w.h($1\leqslant n \leqslant 10, 1\leqslant w,h \leqslant n$),求能放在w*h网格里的不同的n连块的个数(注意,平移.旋转.翻转 ...

  8. Nginx 对上游使用SSL链接

    L96 双向认证SSL指令示列 对下游使用证书指令 Syntax: ssl_certificate file; Default: — Context: http, server Syntax: ssl ...

  9. Go For It ,一个灵活的待办事项列表程序

    导读 Go For It,是我们开源工具系列中的第十个工具,它将使你在 2019 年更高效,它在 Todo.txt 系统的基础上构建,以帮助你完成更多工作. 每年年初似乎都有疯狂的冲动想提高工作效率. ...

  10. Windows下安装Ubuntu 16.04双系统

    本文已有更新:新文章 [2016-05-09 更新说明: ①:我原本写的Ubuntu 16.04安装博客中在安装系统时,在引导项部分,有一点问题没有注意到,感谢@小段阿誉的指出,在下面我有了说明: ② ...