题目描述:

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. Redis 开发规范

    本文主要介绍在使用阿里云Redis的开发规范,从下面几个方面进行说明. 键值设计 命令使用 客户端使用 相关工具 通过本文的介绍可以减少使用Redis过程带来的问题. 一.键值设计 1.key名设计 ...

  2. Linux批量结束、杀死进程

    ps aux|grep python|grep -v grep|cut -c 9-15|xargs kill -15 管道符“|”用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令的输入.下面 ...

  3. lock 单例模式

    单例模式只允许创建一个对象,因此节省内存,加快对象访问速度,因此对象需要被公用的场合适合使用,如多个模块使用同一个数据源连接对象等等 网站的计数器,一般也是采用单例模式实现,否则难以同步 单例模式要素 ...

  4. ls file less

    ls-a 列出所有文件,包含隐藏文件-l 以长格式显示结果-F 在所列出的文件后面显示其格式-r 按照文件名降序展示-t 按照时间列出-S 按照文件大小排序 file 文件名:展示文件的类型简单描述 ...

  5. EntityFramwork 七七八八

    Tip 技术的选型受技术先进性.技术持续性.技术普及度.推行力度的影响. 我也很无奈,一大把年纪了又要重新学一种ORMapping框架. 说实话,这是我用过的最复杂的ORMapping框架了. Ent ...

  6. 2018-2019-2 《网络对抗技术》Exp2 后门原理与实践

    2018-2019-2 <网络对抗技术>Exp2 后门原理与实践 1. 后门原理与实践实验说明及预备知识 一.实验说明 任务一:使用netcat获取主机操作Shell,cron启动 (0. ...

  7. 小米手机跨域问题,返回resphone:undefined,status 0

    小米手机跨域问题,返回resphone:undefined,status 0我小米note2的手机登录不上,返回resphone:undefined,status 0 我手机登录不了的问题解决了,后台 ...

  8. 第01节:ActiveMQ入门和消息中间件

    1.ActiveMQ最主要的功能:实现JMS Provider,用来帮助实现高可用.高性能.可伸缩.易用和安全的企业级面向消息服务的系统.是一个异步的功能. 2.ActiveMQ特点: 完全支持JMS ...

  9. React Native之常用组件(View)

    一. JSX和组件的概念 React的核心机制之一就是虚拟DOM:可以在内存中创建的虚拟DOM元素.React利用虚拟DOM来减少对实际DOM的操作从而提升性能.传统的创建方式是: 但这样的代码可读性 ...

  10. vue 去中心化的路由拆分方案:require.context

    代码地址:https://github.com/lisiyizu/vue-router-dynamic