桑海

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Max Sum
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 49213    Accepted Submission(s): 10950


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

 

 

MyCode·超时
 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4 
 5 #define P(x) \
 6 cout << #x " " << x << ":" << endl;
 7 
 8 #define PE(max, beg, end) \
 9 cout << max << " " << beg+1 << " " << end+1 << endl;
10 
11 int max_sum(int &beg, int &end, vector<int> ivec)
12 {
13     while(ivec[beg] < 0 && beg < ivec.size())
14     {
15         //忽略首相的负数
16         beg++;
17     }
18     int max =  0, now = 0;
19     for(int i = beg; i < ivec.size(); ++i)
20     {
21         now += ivec[i];
22         if(max < now)
23         {
24             max =  now;
25             end = i;
26         }
27     }
28     return max;
29 }
30 int main()
31 {
32     int T, Case = 1;
33     cin >> T;
34     while(T--)
35     {
36         vector<int> ivec;
37         int n, x;
38         cin >> n;
39         while(n--)
40         {
41             cin >> x;
42             ivec.push_back(x);
43         }
44         int beg = 0, end = 0, now_beg = 0, now_end = 0, now = -1000, max = -1000;
45         for(; now_beg < ivec.size(); ++now_beg)
46         {
47             now = max_sum(now_beg, now_end, ivec);
48             if(max < now)
49             {
50                 max = now;
51                 beg = now_beg;
52                 end = now_end;
53             }
54         }
55         P(Case);
56         Case++;
57         PE(max, beg, end);
58         if(T)
59             cout << endl;
60     }
61     return 0;
62 }

 

网上搜索的参考
 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int T,N,num,startP,endP;
 6     cin>>T;
 7     for(int k=0;k<T;k++)
 8     {
 9         cin>>N;
10         int max=-1001,sum=0,temp=1;
11         for(int i=0;i<N;i++)
12         {
13             cin>>num;
14             sum+=num;
15             if(sum>max)
16             {
17                 max=sum;
18                 startP=temp;
19                 endP=i+1;
20             }
21             if(sum<0)
22             {
23                 sum=0;
24                 temp=i+2;
25             }
26         }
27         cout<<"Case "<<k+1<<":"<<endl<<max<<" "<<startP<<" "<<endP<<endl;
28         if(k!=T-1) cout<<endl;
29     }
30     return 0;
31 }

 

posted on 2013-02-23 00:19  桑海  阅读(323)  评论(0编辑  收藏  举报