PTA 1007 Maximum Subsequence Sum (25 分)

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string.h>
 4 #include <vector>
 5 #include <algorithm>
 6 #include <cassert>
 7 #include <queue>
 8 using namespace std; 
 9 int n; 
10 int main()
11 {
12         cin >> n;
13        
14         vector<int> vec(n);
15         // for(int i = 0 ;i< n;++i)
16         // {
17         //         int x;
18         //         cin >> x;
19         //         vec.push_back(x); 
20         // }
21         int temp = 0,left = 0,right = n - 1,sum = -1; 
22         int tempidx = 0;
23         for(int i = 0;i < n; ++i)
24         {
25                 cin >> vec[i];
26                 temp += vec[i];
27                 if(temp < 0)
28                 {
29                         temp = 0;
30                         tempidx = i + 1;
31                 }
32                 else if(temp > sum)
33                 {
34                         sum = temp;
35                         right = i;
36                         left = tempidx;
37                 }
38         }
39         if(sum < 0)
40         sum = 0;
41 
42         cout << sum << " " << vec[left] << " " << vec[right] << endl;
43 } 

 

posted @ 2019-08-08 11:58  冷眼旁观你的泪  阅读(365)  评论(0编辑  收藏  举报