wa了一次,犯了个很经典的错误——直接把R设成了所有体力值的sum
然而check函数里只有mid不合法的时候才会返回false……R指向的永远是不可能的点
下次注意……
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
const int MAXN = 1000000 + 50;
LL n,A[MAXN],k;
LL L,R;
bool check(LL x)
{
LL cnt = 0,now = 0;
for(int i = 1;i <= n;i ++)
{
if(now < x)now += A[i];
if(now >= x)
{
now = 0;
cnt ++;
if(cnt >= k)return true;
}
}
return false;
}
int main()
{
scanf("%lld%lld",&n,&k);
for(int i = 1;i <= n;i ++)
{
scanf("%lld",&A[i]);
R += A[i];
}
R ++;
while(R - L > 1)
{
LL mid = L + R >> 1;
if(check(mid))L = mid;
else R = mid;
}
printf("%lld",L);
return 0;
}