zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    课堂作业
    大道至简读后感
    读《大道至简》有感
    大道至简第四章-流于形式的沟通
    Java课堂动手动脑-截图集锦
    Java动手动脑课后作业1-求创建对象个数
    Java-消息框显示两整数加减乘除
    JAVA-实践问题
    Java-整数相加求和
    大道至简-是懒人造就了方法
  • 原文地址:https://www.cnblogs.com/xuzhiyuan/p/7815489.html
Copyright © 2011-2022 走看看