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 }
  • 相关阅读:
    MySQL字符串中数字排序的问题
    Jquery 实现跨域处理
    mysql中insert into select from的使用
    Mysql索引会失效的几种情况分析(转)
    ASP.Net Web API 输出缓存(转)
    8 种提升 ASP.NET Web API 性能的方法 (转)
    C#进阶系列——WebApi 异常处理解决方案(转)
    C#使用Log4Net记录日志(转)
    前端传递对象列表到WebApi
    忏悔录
  • 原文地址:https://www.cnblogs.com/xuzhiyuan/p/7815489.html
Copyright © 2011-2022 走看看