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[100001]; void read(int &x) { x=0; int f=1; char c=getchar(); while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); } while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); } x*=f; } int check(long long k) { long long now=0; int sum=0; for(int i=1;i<=n;++i) { now+=x[i]; if(now>=k) sum++,now=0; else if(now<0) now=0; } return sum; } int main() { freopen("autoac.in","r",stdin); freopen("autoac.out","w",stdout); read(n); read(m); for(int i=1;i<=n;++i) read(x[i]); long long l=1,r=1LL*n*1e9,tmp=-1,mid; int t; while(l<=r) { mid=l+r>>1; t=check(mid); if(t==m) tmp=mid,r=mid-1; else if(t>m) l=mid+1; else r=mid-1; } if(tmp==-1) { printf("-1"); return 0; } cout<<tmp<<' '; l=tmp,r=1LL*n*1e9; while(l<=r) { mid=l+r>>1; t=check(mid); if(t==m) tmp=mid,l=mid+1; else if(t>m) l=mid+1; else r=mid-1; } cout<<tmp; return 0; }