第1行:整数序列的长度N(2 <= N <= 50000) 第2 - N+1行:N个整数
输出最小正子段和。
8 4 -1 5 -2 -1 2 6 -2
1
#include <algorithm> #include <cstdio> using namespace std; int n; long long minx=1e18; long long min(long long a,long long b) { return a>b?b:a; } struct node { long long zdh; int pos; bool operator<(node a)const { if(zdh!=a.zdh) return zdh<a.zdh; else return pos<a.pos; } }sum[50050]; int main() { scanf("%d",&n); for(int a,i=1;i<=n;i++) { scanf("%d",&a); sum[i].zdh=sum[i-1].zdh+a; sum[i].pos=i; } sort(sum+1,sum+1+n); if(sum[1].zdh>=1) minx=sum[1].zdh; for(int i=2;i<=n;i++) { if(sum[i].zdh>=1) minx=min(minx,(long long)sum[i].zdh); if(sum[i].pos>sum[i-1].pos&&sum[i].zdh-sum[i-1].zdh>=1) minx=min(minx,sum[i].zdh-sum[i-1].zdh); } printf("%lld",minx); return 0; }