HDOJ 1003

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 85953    Accepted Submission(s): 19912


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 #include <iostream>
 2 using namespace std;
 3 
 4 
 5 int main()
 6 {
 7         int numbers;
 8         cin>>numbers;
 9         for(int k =0;k<numbers;k++)
10         {
11                 int num;
12                 cin>>num;
13                 
14                 
15                         int * number = new int [num];
16                         int * start =  new int [num];
17                         int * b =  new int [num];
18                         for(int i=0;i<num;i++)
19                                 cin>>number[i];
20                         
21                         b[0]= number[0];
22                         start[0]=0;
23                         for(int i=1;i<num;i++)
24                         {
25                                 if(b[i-1]+number[i]>=number[i])
26                                 {
27                                         b[i]=b[i-1]+number[i];
28                                         start[i]= start[i-1];
29                                 }
30                                 else
31                                 {
32                                         b[i]= number[i];
33                                         start[i]= i;
34                                 }
35                         }
36                         int max = b[0];
37                         int pos=0;
38                         for(int i=0;i<num;i++)
39                         {
40                                 if(b[i]>max)
41                                 {
42                                         max=b[i];
43                                         pos = i;
44                                 }
45                         }
46                         cout<<"Case "<<k+1<<":"<<endl<< max<<" "<<start[pos]+1<<" "<<pos+1<<endl; 
47                         if(k!=numbers-1)
48                                 cout<<endl;
49                         delete number;
50                         delete start;
51                         delete b;
52 
53 
54         }
55 
56 
57         return 0;
58 
59 
60 
61 }

 

posted @ 2012-09-19 15:32  xingoo  阅读(529)  评论(0编辑  收藏  举报