51Nod 1065 最小正子段和

 1 #include <iostream>
 2 #include <algorithm>
 3 
 4 using namespace std;
 5 const int maxn = 50000 + 5;
 6 struct node{
 7     long long x;
 8     int id;
 9 }a[maxn];
10 
11 bool cmp(node xx, node yy){
12     if (xx.x == yy.x)
13         return xx.id < yy.id;
14     else return xx.x < yy.x;
15 }
16 
17 int main(){
18     ios::sync_with_stdio(false);
19     int n;
20     cin >> n;
21     int xx;
22     a[0].id = 0;
23     a[0].x = 0;
24     for (int i = 1; i <= n; i++){
25         cin >> xx;
26         a[i].x = a[i - 1].x + xx;
27         a[i].id = i;
28     }
29     sort(a, a + n + 1, cmp);
30     long long ans;
31     bool flag = true;
32     for (int i = 1; i <= n; i++){
33         if (a[i].x - a[i - 1].x > 0 && a[i].id - a[i - 1].id > 0){
34             if (flag){
35                 ans = a[i].x - a[i - 1].x;
36                 flag = false;
37             }
38             else{
39                 ans = min(ans, a[i].x - a[i - 1].x);
40             }
41         }
42     }
43     cout << ans << endl;
44     system("pause");
45     return 0;
46 }

 

posted @ 2018-04-02 20:35  ouyang_wsgwz  阅读(137)  评论(0编辑  收藏  举报