BZOJ 4590 [Shoi2015]自动刷题机 ——二分答案

二分答案水题。

#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\n");
    else printf("%lld %lld\n",minn,maxx);
}

  

posted @ 2017-05-05 16:14  SfailSth  阅读(161)  评论(0编辑  收藏  举报