标签:整数 \n sort 关系 puts AC iterator nod --
输出最小的正子段和
3
-1 2 3
1
思路
记录前缀和,然后排序,满足前缀和之差最小和下标递增的关系就行,注意longlong!!longlong!!
#include <bits/stdc++.h> using namespace std; const int N = 50005; typedef long long LL; struct node { LL x, y; } q[N]; bool cmp(node a, node b) { if(a.x == b.x) return a.y < b.y; return a.x < b.x; } int main() { int n, x; cin>>n; for(LL i = 1; i <= n; i++) { scanf("%d", &x); q[i].x = q[i-1].x + x; q[i].y = i; } sort(q+1, q+1+n, cmp); LL ans = 1e18; int base = 0; for(int i = 1; i <= n; i++) { if(q[i].x > q[base].x && q[i].y > q[base].y) { ans = min(ans, q[i].x - q[base].x); base++; i = base; } } if(ans == 1e18) puts("no solution"); else printf("%lld\n", ans); return 0; }
也可以利用set和lower_bound写
#include <bits/stdc++.h> using namespace std; const int N = 50005; typedef long long LL; LL a[N]; set<LL>S; int main() { int n, x; cin>>n; for(int i = 1; i <= n; i++) { scanf("%d", &x); a[i] = a[i-1] + x; } S.clear(); LL ans = 1e18; for(int i = 1; i <= n; i++) { S.insert(a[i]); if(a[i] > 0) ans = min(ans, a[i]); set<LL>::iterator it = S.lower_bound(a[i]); if(it == S.begin()) continue; it--; if(a[i] - (*it) <= 0) continue; ans = min(ans, a[i] - (*it)); } if(ans == 1e18) puts("no solution"); else printf("%lld\n", ans); return 0; }
【zznu-2060】 Minsum Plus(最小正子段和)
标签:整数 \n sort 关系 puts AC iterator nod --
原文地址:https://www.cnblogs.com/lesroad/p/9066515.html