http://www.lydsy.com/JudgeOnline/problem.php?id=4590
二分
这么道水题 没long long WA了两发,没判-1WA了一发,二分写错WA了一发
最近是怎么了啊啊啊O(≧口≦)O
#include<cstdio>
#include<iostream> using namespace std; int n,m; int x[]; void read(int &x)
{
x=; int f=; char c=getchar();
while(!isdigit(c)) { if(c=='-') f=-; c=getchar(); }
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
x*=f;
} int check(long long k)
{
long long now=;
int sum=;
for(int i=;i<=n;++i)
{
now+=x[i];
if(now>=k) sum++,now=;
else if(now<) now=;
}
return sum;
} int main()
{
freopen("autoac.in","r",stdin);
freopen("autoac.out","w",stdout);
read(n); read(m);
for(int i=;i<=n;++i) read(x[i]);
long long l=,r=1LL*n*1e9,tmp=-,mid;
int t;
while(l<=r)
{
mid=l+r>>;
t=check(mid);
if(t==m) tmp=mid,r=mid-;
else if(t>m) l=mid+;
else r=mid-;
}
if(tmp==-)
{
printf("-1");
return ;
}
cout<<tmp<<' ';
l=tmp,r=1LL*n*1e9;
while(l<=r)
{
mid=l+r>>;
t=check(mid);
if(t==m) tmp=mid,l=mid+;
else if(t>m) l=mid+;
else r=mid-;
}
cout<<tmp;
return ;
}