Max Sum

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

Total Submission(s): 251171    Accepted Submission(s): 59503

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
Problem : 1003 ( Max Sum )     Judge Status : Accepted

RunId : 21239601    Language : G++    Author : hnustwanghe

Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta

#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;
const int INF = -(1<<30);

int main(){
int T,n,cnt = 0;
scanf("%d",&T);
while(T--){
int ans ,start=1,tail,tmp = 0, x,ts;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&x);
if(i==1){
ans = tmp = x;
start = tail = ts = 1;
}
else{
if(x > x + tmp){
tmp = x;
ts = i;
}
else tmp = tmp + x;
}
if(tmp > ans ){
ans = tmp;
start = ts,tail = i;
}
}
printf("Case %d:\n%d %d %d\n",++cnt,ans,start,tail);
if(T) printf("\n");
}
}

#include<iostream>
#include<cstring>
#include<cstdio>

using namespace
std;
const int
INF = -(1<<30); int main(){
int
T,n,cnt = 0;
scanf("%d",&T);
while(
T--){
int
ans ,start=1,tail,tmp = 0, x,ts;
scanf("%d",&n);
for(int
i=1;i<=n;i++){
scanf("%d",&x);
if(
i==1){
ans = tmp = x;
start = tail = ts = 1;
}
else{
if(
x > x + tmp){
tmp = x;
ts = i;
}
else
tmp = tmp + x;
}
if(
tmp > ans ){
ans = tmp;
start = ts,tail = i;
}
}

printf("Case %d:\n%d %d %d\n",++cnt,ans,start,tail);
if(
T) printf("\n");
}
}

 

动态规划: HDU1003Max Sum的更多相关文章

  1. HDU1003MAX SUM (动态规划求最大子序列的和)

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

  2. 动态规划:HDU1003-Max Sum(最大子序列和)

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

  3. 动态规划——Maximum Sum of 3 Non-Overlapping Subarrays

    这个题对我来说真的是相当难的题目了,严格来讲可能不算是个动态规划的题目,但这个题目对类似的划分多个非重叠连续子区间的问题提供了一个很好解决方案 这个题目需要找三个非重叠的连续子区间,通过维护两个数组将 ...

  4. HDU1003MAX SUM

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

  5. 动态规划-Largest Sum of Averages

    2018-07-12 23:21:53 问题描述: 问题求解: dp[i][j] : 以ai结尾的分j个部分得到的最大值 dp[i][j] = max{dp[k][j - 1] + (ak+1 + . ...

  6. C++-HDU1003-Max Sum

    时间复杂度O(n) 空间复杂度O(1) #include <cstdio> int main() { int T;scanf("%d",&T); ,n,a,l, ...

  7. HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  8. 43. 动态规划求解n个骰子的点数和出现概率(或次数)[Print sum S probability of N dices]

    [题目] 把N个骰子扔在地上,所有骰子朝上一面的点数之和为S.输入N,打印出S的所有可能的值出现的概率. [分析] 典型的动态规划题目. 设n个骰子的和为s出现的次数记为f(n,s),其中n=[1-N ...

  9. HDOJ-1003 Max Sum(最大连续子段 动态规划)

    http://acm.hdu.edu.cn/showproblem.php?pid=1003 给出一个包含n个数字的序列{a1,a2,..,ai,..,an},-1000<=ai<=100 ...

随机推荐

  1. python1--比较三个数值的大小

    num1 = input("num1:")num2 = input("num2:")num3 = input("num3:")num = m ...

  2. spring自带工具类

    在spring-core.jar包中,org.springframework.util package下有很多工具类,这些工具类十分具有参考意义.

  3. 消息队列之--RocketMQ

    序言 资料 https://github.com/alibaba/RocketMQ http://rocketmq.apache.org/

  4. SpringCloud 入门知识篇

    SpringCloud 入门 springcloud 学习 7天学会springcloud 教程 https://www.cnblogs.com/skyblog/category/738524.htm ...

  5. Spring Boot教程(九)异步方法

    创建工程 在pom文件引入相关依赖: <dependency> <groupId>org.springframework.boot</groupId> <ar ...

  6. Integer类源码浅析

    1.首先Integer提供了两类工具类,包括把一个int类型转成二进等, 其实执行转换算法只有一个方法: public static String toString(int i, int radix) ...

  7. hbuilderx 连接模拟器

    打开cmd,找到bin的安装目录(G:\Program Files\Nox\bin)       cd进入夜神模拟器bin目录 执行以下命令 nox_adb connect 127.0.0.1:620 ...

  8. webrtp官方demo运行

    Google官方提供的webrtc的demo对应的网站是https://webrtc.github.io/samples/ 上面的DEMO对我这种入门的人很有用,用chrome浏览器最新的版本直接可以 ...

  9. post与get方法的区别

    1. GET请求:请求的数据会附加在URL之后,以?分割URL和传输数据,多个参数用&连接.URL的编码格式采用的是ASCII编码,而不是unicode,即是说所有的非ASCII字符都要编码之 ...

  10. easyhook源码分析二——注入

    EasyHook 中的注入方法. 函数原型 // EasyHook 中的命名比较有意思,Rh 代表的就是Remote Hook,此函数就是远程钩子的一个子过程----注入,前面的宏代表它是导出函数. ...