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. css-js-弹出层

    HTML: <!-- 弹出层 --> <div class="popwindow" > <div class="pop" id=& ...

  2. linq 分页

    urList = (from u in urList                      orderby u.toolingNo_C                      select u) ...

  3. 配置MongoDB的Windows服务

    [1] 创建directorys和files Create a configuration file and a directory path for MongoDB log output (logp ...

  4. 将Emacs Org mode用于GTD任务管理

    在上一篇日志中,我简要介绍了如何围绕Emacs Org mode构建个人任务管理系统的基本思路与方法.因为Org mode体系庞大.功能繁杂,本文仅以提纲契领的方式介绍不同环节在Org mode中的操 ...

  5. Tire树简介

    又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种. 典型应用:用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计. 它的优点是:利用字符串的公共 ...

  6. 洛谷 P2330 [SCOI2005] 繁忙的都市 x

    题目描述 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条 ...

  7. FJOI2017 day2游记

    day0 早上复习了一下凸包,lct的板子,发现现在的我好菜鸡啊,做题基本上还得看题解,自己不是很能分析出来. 下午去看考场,在附中机房又写了一遍lct,然后a掉了文理分科完就回去了. 回家的路上走在 ...

  8. CSS 阴影应用

    1.背景阴影的使用 box-shadow:1px 1px 1px 1px #ccccccc inset; 说明:第一个参数为水平阴影的大小.第二个为垂直阴影的大小(值可以为负数).第三个参数是阴影模糊 ...

  9. vue之Object.defineProperty()

    了解Object.defineProerty()方法 关于Object.defineProperty()方法的解释,理解Object.defineProperty的作用 这篇文章做了很详细的概述 关于 ...

  10. 简单记录一下vue生命周期及 父组件和子组件生命周期钩子执行顺序

    首先,vue生命周期可以用下图来简单理解 当然这也是官方文档的图片,详细的vue周期详解请参考这里 然而当同时存在父子组件的时候生命周期钩子是如何执行的呢? 请看下文: 加载渲染过程父beforeCr ...