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. “_MSC_VER”的不匹配项

    近些年来vs更新步伐加快,深刻的感受到了技术成长学习的重要性. 另一方面,版本的更换,也带来了许多的问题.今天用2019打开以前2010的工程时就碰到了一个: 检测到“_MSC_VER”的不匹配项: ...

  2. android stadio gradle问题

    https://www.jianshu.com/p/2bb0b6a7b479 https://www.jianshu.com/p/d175bef9770c Unable to resolve depe ...

  3. socket模块其他用法

    1.socket模块的其他方法 服务端套接字函数 s.bind() 绑定(主机,端口号)到套接字 s.listen() 开始TCP监听 s.accept() 被动接受TCP客户的连接,(阻塞式)等待连 ...

  4. Spring Data Jpa (二)JPA基础查询

    介绍Spring Data Common里面的公用基本方法 (1)Spring Data Common的Repository Repository位于Spring Data Common的lib里面, ...

  5. spring boot shiro redis整合基于角色和权限的安全管理-Java编程

    一.概述 本博客主要讲解spring boot整合Apache的shiro框架,实现基于角色的安全访问控制或者基于权限的访问安全控制,其中还使用到分布式缓存redis进行用户认证信息的缓存,减少数据库 ...

  6. leetcode 回文二叉树

    C++最简单的方法,遍历存在vector<int> ivec容器中,然后头尾对应比较 O(n)时间,O(n)空间 /** * Definition for singly-linked li ...

  7. Windows程序调用dll

    可以写在WndProc的WM_CREATE里面,不能写在WinMain里面

  8. Spring 之 IOC ,DI 理论

    本文是依照极客学院java<Spring之IOC>章节学习的心得.随笔记录 浅谈IOC:(Inversion of Control, 控制反转) Spring 核心容器,贯穿始终.所谓IO ...

  9. 阶段3 1.Mybatis_07.Mybatis的连接池及事务_2 连接池介绍

  10. Apache监控调优

    apache是一款对静态资源处理得比较好的中间件,但是对动态请求处理得不是很好,tomcat则正好相反. apache运用得比较多得工作模式主要是Prefork和Worker两种模式 1.Prefor ...