• hdu 1003 Max Sum(dp)


    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
     
     1 import java.util.Scanner;
     2 
     3 public class Test4 {
     4 
     5     public static void main(String[] args) {
     6         
     7         int[] dp=new int[100005];
     8         int[] arrs=new int[100005];
     9         
    10         Scanner input=new Scanner(System.in);
    11         
    12         int T=input.nextInt();//测试用例个数
    13         for(int i=0;i<T;i++) {
    14             
    15             int start,end,left,submax;
    16             
    17             int n=input.nextInt();
    18             for(int j=0;j<n;j++) {
    19                 
    20                 arrs[j]=input.nextInt();
    21                 
    22             }
    23             
    24             dp[0]=submax=arrs[0];
    25             start=end=left=1;
    26             
    27             for(int j=1;j<n;j++) {
    28                 
    29                 if(arrs[j]>arrs[j]+submax) {
    30                     
    31                     submax=arrs[j];
    32                     left=j+1;
    33                     
    34                 }else {
    35                     
    36                     submax=submax+arrs[j];
    37                     
    38                 }
    39                 
    40                 if(dp[j-1]>=submax) {
    41                     
    42                     dp[j]=dp[j-1];
    43                     
    44                 }else {
    45                     
    46                     dp[j]=submax;
    47                     start=left;
    48                     end=j+1;
    49                     
    50                 }
    51                 
    52             }
    53             
    54             System.out.println("case"+(i+1)+":"+dp[n-1]+" "+left+" "+end);
    55             
    56         }
    57 
    58     }
    59 
    60 }
  • 相关阅读:
    Linux下用命令格式化U盘
    ABAP
    [Java 并发] Java并发编程实践 思维导图
    html和css实现一级菜单和二级菜单学习笔记
    小贝_mysql建表以及列属性
    Android.mk具体解释
    Maven之——坐标和依赖(上)
    让 Nginx 支持 WAF 防护功能web防火墙
    EZHTTP首页、文档和下载
    http://www.sshguard.net/
  • 原文地址:https://www.cnblogs.com/xuzhiyuan/p/7815489.html
Copyright © 2020-2023  润新知