题目描述:

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,不知道错误在哪里
 
 
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. Scanner cin = new Scanner(System.in);
  7. int[] array = new int[100010];
  8. int[] dp = new int[100010];
  9. int t = cin.nextInt();
  10. for (int count = 0; count < t; count++) {
  11.  
  12. int n = cin.nextInt();
  13. for(int i = 0;i<n;i++){
  14. array[i] = cin.nextInt();
  15. }
  16.  
  17. dp[0] = array[0];
  18.  
  19. for(int i =1;i<n;i++){
  20.  
  21. dp[i] = Math.max(dp[i-1]+array[i], array[i]);
  22. }
  23.  
  24. int max = dp[0];
  25.  
  26. int endIndex = 0;
  27.  
  28. for(int i = 1;i<n;i++){
  29. if(dp[i]>max){
  30. max = dp[i];
  31. endIndex = i;
  32. }
  33. }
  34.  
  35. int temp =0,l = endIndex;
  36.  
  37. for(int i = endIndex;i>=0;i--){
  38. temp+=array[i];
  39. if(temp==max){
  40. l = i;
  41. }
  42. }
  43.  
  44. if(count!=0){
  45. System.out.println();
  46. }
  47. System.out.println("Case "+(count+1)+":");
  48. System.out.println(max+" "+(l+1)+" "+(endIndex+1));
  49. }
  50. }
  51.  
  52. }
 

《 动态规划_ 入门_最大连续子序列_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. 使用pushstate,指定回退地址

    history.pushState(null,"testname", window.location.href); window.addEventListener('popstat ...

  2. 记录一个bootstrap惨痛的错误

    记录一个bootstrap的错误,这个错误因为我删除了一个class就导致了页面上显示的错误,这是一个惨痛的教训,特此记录,提醒自己在做前端的修改时,一定要慎之又慎.如果真的要做改动,改完之后也要测一 ...

  3. allegro17.2 错误记录

    1.网表导入PCB时出现如下错误::error with pin number 'P11'in device ' lfbga176'::Unable to find pin name in adfnc ...

  4. 【Linux】-NO.160.Linux.1 -【升级Centos7】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...

  5. python smtp 发邮件 添加附件

    # -*- coding:utf-8 -*- # __author__ = 'justing' import os import smtplib from email.mime.multipart i ...

  6. [USACO11DEC]牧草种植Grass Planting

    图很丑.明显的树链剖分,需要的操作只有区间修改和区间查询.不过这里是边权,我们怎么把它转成点权呢?对于E(u,v),我们选其深度大的节点,把边权扔给它.因为这是树,所以每个点只有一个父亲,所以每个边权 ...

  7. php普通传值和引用传值 (相当通俗易懂的一篇讲解)

    首先,要理解变量名存储在内存栈中,它是指向堆中具体内存的地址,通过变量名查找堆中的内存; 普通传值,传值以后,是不同的地址名称,指向不同的内存实体; 引用传值,传引用后,是不同的地址名称,但都指向同一 ...

  8. 第九届蓝桥杯C/C++B组省赛感想

    因为做了近三年的初赛题,都对了5题+,所以这次比赛前信心满满,心里想最少水个省二没问题.可我怎么知道今年的套路居然和以前不一样了!一题深搜都没有,想想一周前做的第七届初赛题,10题有3.4题深搜题. ...

  9. 克隆Rockey6加密狗复制资料

    克隆Rockey6加密狗复制资料下载 描述:Rockey6加密狗复制克隆方法Rockey6加密狗复制案例解析! 一.用OD加载DLL,并分析: 10001320 >/$ B8 4C140000 ...

  10. sedlauncher.exe

    这个进程很恐怖,屁大点的东西,但会造成磁盘爆满. 首先,这个99%不是病毒,而是win10更新后出现的东西. 关于解释,国内乱七八糟的,我没有搜到,只好在狗哥和微软官网搜了一下. 大多说是 KB402 ...