# Max Sum

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

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

Author

Ignatius.L

http://acm.hdu.edu.cn/showproblem.php?pid=1003

 1 #include<iostream>
2 #include<vector>
3 using namespace std;
4 int main()
5 {
6     int t,n,i,k,st,en,maxn,sum,flag=1;
7     cin>>t;
8     while(t--)
9     {
10         cin>>n;
11         vector<int>arr(n);
12         for(i=0;i<n;i++)
13             cin>>arr[i];
14           st=en=sum=0;
15         for(i=0,k=0;i<n;i++)
16          {
17             sum+=arr[i];
18             if(i==0||sum>maxn)
19            {
20                 maxn=sum;
21                 en=i;
22                 st=k;
23            }
24           if(sum<0)
25            {
26              sum=0;
27              k=i+1;
28            }
29          }
30          cout<<"Case "<<flag++<<":"<<endl;
31          cout<<maxn<<" "<<st+1<<" "<<en+1<<endl;
32          if(t!=0)cout<<endl;
33     }
34     return 0;
35 }

657 篇文章64 人订阅

0 条评论

## 相关文章

### ZOJ 3607 Lazier Salesgirl (枚举)

Lazier Salesgirl Time Limit: 2 Seconds Memory Limit: 65536 KB Kochiya S...

37670

23570

### HDUOJ----2647Reward

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja...

37080

### HDUOJ------Daydream字符查找-并求其始末位置

2013-07-17  10:50:38 Daydream Time Limit: 2000/1000 MS (Java/Others)    Memory L...

29360

### hdu 4009 Transfer water（最小型树图）

Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/657...

37990

### HDU 1103 Flo's Restaurant(模拟+优先队列)

Flo's Restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/...

29360

### poj----Ubiquitous Religions

Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submi...

37760

### hdu-----(1507)Uncle Tom's Inherited Land*(二分匹配)

Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Lim...

26540

### hdu----(5055)Bob and math problem(贪心)

Bob and math problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327...

29550

### HDUOJ 1099——Lottery

Lottery Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J...

37170