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. 计时器 GC垃圾回收 demo

    计时器: public void start() { //定义计时器 Timer timer=new Timer(); //定义运行间隔(数字越小,速度越快) int interval=30; //创 ...

  2. ClustrixDB安装配置

    前提条件 在安装ClustrixDB之前,需要: ClustrixDB安装程序和许可证密钥. 运行CentOS或RHEL 7.4的服务器(本地或云中). 具有root或sudo特权来安装Clustri ...

  3. JAVA语言动手动脑问题

    1. 早期经常这样定义变量  int value=100;前面的示例中这样定义变量  MyClass obj = new MyClass(); 这两种方式定义的变量是一样的吗?   答:不一样,后者开 ...

  4. JMS学习二(简单的ActiveMQ实例)

    下载安装ActiveMQ服务,下载地址当然可以去官网下载 http://activemq.apache.org/download-archives.html ActiveMQ安装很简单,下载解压后到b ...

  5. Spring Boot教程(六)在springboot中验证表单信息

    构建工程 创建一个springboot工程,由于用到了 web .thymeleaf.validator.el,引入相应的起步依赖和依赖,代码清单如下: <dependencies> &l ...

  6. Spring Cloud Stream教程(一)介绍Spring Cloud Stream

    Spring Cloud Stream是构建消息驱动的微服务应用程序的框架.Spring Cloud Stream基于Spring Boot建立独立的生产级Spring应用程序,并使用Spring I ...

  7. 测试版和正式版微信小程序共享存储空间问题

    一般习惯将变量存储在小程序的storage缓存中,然后用到的时候再去取.但是有一次我在做小程序相关内容的时候发现,对于苹果手机,测试版本小程序和正式版本小程序的缓存变量是相互通用的.

  8. imu tool使用

    安装imu tool sudo apt-get install ros-melodic-imu-tools launch文件: <!-- imu_node launch file--> & ...

  9. 如何将post请求转换成put和delete请求

    <form:form action="${pageContext.request.contextPath}/emp" method="POST" mode ...

  10. java利用zip解压slpk文件

    public static void main(String[] args) { File file = new File("C:\\Users\\Administrator\\Deskto ...