【bzoj4590】[Shoi2015]自动刷题机
因为解一定是单调的,n越小切的题越多,这是可以肯定的,那么直接二分答案
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<queue> 8 using namespace std; 9 10 typedef long long LL; 11 12 #define INF 1LL<<60 13 #define N 100010 14 15 int n,k; 16 LL l,r,L,R; 17 18 LL a[N]; 19 20 LL work(LL x) 21 { 22 LL res(0),cnt(0); 23 for (int i=1;i<=n;i++) 24 { 25 res=max(res+a[i],0LL); 26 if (res>=x) 27 28 res=0,cnt++; 29 } 30 return cnt; 31 } 32 33 int main() 34 { 35 scanf("%d%d",&n,&k); 36 for (int i=1;i<=n;i++) 37 scanf("%lld",&a[i]); 38 l=1; 39 r=INF; 40 while (l<=r) 41 { 42 LL m=(l+r)>>1; 43 if (work(m)>k) 44 L=m,l=m+1; 45 else 46 r=m-1; 47 } 48 l=1; 49 r=INF; 50 while (l<=r) 51 { 52 LL m=(l+r)>>1; 53 if (work(m)<k) 54 R=m,r=m-1; 55 else 56 l=m+1; 57 } 58 L++; 59 R--; 60 if (work(L)!=k || work(R)!=k) 61 printf("-1\n"); 62 else 63 printf("%lld %lld",L,R); 64 return 0; 65 }