hdu 1003 Max Sum(dp)

Posted xzy不会飞的地板流

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

以上是关于hdu 1003 Max Sum(dp)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1003 Max Sum (dp)

hdu 1003 Max Sum(dp)

hdu1003 Max Sum最大连续子序列之和

HDU - 1003 Max Sum(DP经典题2)

HDU1003 Max Sum 解题报告

HDU 1003 - Max Sum