二分答案水题。
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 100005 #define ll long long ll n,k,a[maxn],minn,maxx; #define llinf 10000000000000000LL #define F(i,j,k) for (ll i=j;i<=k;++i) ll check(ll lim) { ll now=0,ret=0; F(i,1,n) { now+=a[i]; if (now<0) now=0; if (now>=lim) now=0,ret++; } return ret; } int main() { scanf("%lld%lld",&n,&k); F(i,1,n) scanf("%lld",&a[i]); ll l=1,r=llinf; while(l<r) { ll mid=(l+r)/2; if (check(mid)<=k) r=mid; else l=mid+1; } minn=r; l=1,r=llinf; while(l<r) { ll mid=(l+r)/2+1; if (check(mid)>=k) l=mid; else r=mid-1; } maxx=l; if (check(minn)!=k&&check(maxx)!=k) printf("-1 "); else printf("%lld %lld ",minn,maxx); }