因为解一定是单调的,n越小切的题越多,这是可以肯定的,那么直接二分答案
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 10 typedef long long LL;11 12 #define INF 1LL<<6013 #define N 10001014 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 else46 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)