题目描述:

Max Sum

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

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
 
 
 
Java 代码实现,其中有个小坑,就是dp数组要开的大些,不然一直WA,不知道错误在哪里
 
 
 import java.util.Scanner;

 public class Main {

     public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int[] array = new int[100010];
int[] dp = new int[100010];
int t = cin.nextInt();
for (int count = 0; count < t; count++) { int n = cin.nextInt();
for(int i = 0;i<n;i++){
array[i] = cin.nextInt();
} dp[0] = array[0]; for(int i =1;i<n;i++){ dp[i] = Math.max(dp[i-1]+array[i], array[i]);
} int max = dp[0]; int endIndex = 0; for(int i = 1;i<n;i++){
if(dp[i]>max){
max = dp[i];
endIndex = i;
}
} int temp =0,l = endIndex; for(int i = endIndex;i>=0;i--){
temp+=array[i];
if(temp==max){
l = i;
}
} if(count!=0){
System.out.println();
}
System.out.println("Case "+(count+1)+":");
System.out.println(max+" "+(l+1)+" "+(endIndex+1));
}
} }
 

《 动态规划_ 入门_最大连续子序列_HDU_1003 》的更多相关文章

  1. python爬虫_入门_翻页

    写出来的爬虫,肯定不能只在一个页面爬,只要要爬几个页面,甚至一个网站,这时候就需要用到翻页了 其实翻页很简单,还是这个页面http://bbs.fengniao.com/forum/10384633. ...

  2. 动态规划(Dynamic Programming, DP)---- 最大连续子序列和

    动态规划(Dynamic Programming, DP)是一种用来解决一类最优化问题的算法思想,简单来使,动态规划是将一个复杂的问题分解成若干个子问题,或者说若干个阶段,下一个阶段通过上一个阶段的结 ...

  3. ACM_HDU 1231 最大连续子序列 (dp)_代码分析

    Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i < ...

  4. Spring_MVC_教程_快速入门_深入分析

    Spring MVC 教程,快速入门,深入分析 博客分类: SPRING Spring MVC 教程快速入门  资源下载: Spring_MVC_教程_快速入门_深入分析V1.1.pdf Spring ...

  5. HDU 1231 最大连续子序列 --- 入门DP

    HDU 1231 题目大意以及解题思路见: HDU 1003题解,此题和HDU 1003只是记录的信息不同,处理完全相同. /* HDU 1231 最大连续子序列 --- 入门DP */ #inclu ...

  6. 09_android入门_采用android-async-http开源项目的GET方式或POST方式实现登陆案例

    根据08_android入门_android-async-http开源项目介绍及使用方法的介绍,我们通过最常见的登陆案例进行介绍android-async-http开源项目中有关类的使用.希望对你学习 ...

  7. 09_android入门_採用android-async-http开源项目的GET方式或POST方式实现登陆案例

    依据08_android入门_android-async-http开源项目介绍及用法的介绍,我们通过最常见的登陆案例进行介绍android-async-http开源项目中有关类的使用.希望对你学习an ...

  8. 使用Java管理千台规模Linux服务器_入门

    http://www.oschina.net/code/snippet_222919_11734 代码分享 当前位置: 代码分享 » Java  » 网络编程 搜 索   [饶过] 使用Java管理千 ...

  9. 【动态规划】最大连续子序列和,最大子矩阵和,最大m子段和

    1.最大字段和问题 求一个序列最大连续子序列之和. 例如序列[-1,-2,-3,4,5,-6]的最大子段和为4 + 5 = 9. ①枚举法 int MaxSum(int n,int *a){ int ...

随机推荐

  1. dddquickly

    在项目开发最初的时候,他也有过一段狂欢般的快乐时光,不久之后,事情就越来越艰难. 项目的代码越来越难以维护,工作越来越像是一种煎熬,合作的同事对他越来越不满. “该是与这个项目,与这个公司说 bye ...

  2. centos7开启80和8080端口

    开启8080端口 firewall-cmd --permanent --add-port=8080/tcp firewall-cmd --reload 重定向80端口到8080端口firewall-c ...

  3. 创建docker公共仓库

    1.注册公共仓库的账号 https://hub.docker.com 账号:user-a 密码:pwd 2.Linux中登录docker公共仓库 docker login 3.上传一个docker镜像 ...

  4. java.lang.IllegalStateException: Connection pool shut down

    最近使用HttpClient 4.5 使用 CloseableHttpClient 发起连接后,使用CloseableHttpResponse 接受返回结果,结果就报错了,上网查了下,有位stacko ...

  5. 浅析对spring中IOC的理解

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  6. Android 浏览器内 H5 电脑 Chrome 调试

    Android 浏览器内 H5 调试 chrome://inspect 移动前端调试方案(Android + Chrome 实现远程调试) adb 相关资源 adb shell(ADB Kits)下载 ...

  7. UGUI-Text——自适应

    Text组件上勾选Best Fit,当内容变多时,按原来大小装不下时,会总体缩放显示

  8. jieba(杰巴)分词的三种模式

    jieba(结巴)是一个强大的分词库,完美支持中文分词,做为最好的Python中文分词组件. 安装:pip install jieba 特点 支持三种分词模式: 1.精确模式,试图将句子最精确地切开, ...

  9. spring boot入门小案例

    spring boot 入门小案例搭建 (1) 在Eclipse中新建一个maven project项目,目录结构如下所示: cn.com.rxyb中存放spring boot的启动类,applica ...

  10. Mac下安装SecureCRT并激活

    今天花了好长的时间终于把SecureCRT安装成功了 现在分享给大家 安装的步骤, 希望对大家用帮助 Mac下的SecureCRT需要破解才能使用 所以有些费劲的.. 先下载SecureCRT和破解文 ...