NOI online judge 月度开销(二分)

NOI online judge 月度开销(二分)

基本二分;
变量名要注意!

const int maxn=100001;
int n,m,cost[maxn];
long long sum1 = 0,maxm = 0;
int main()
{
scanf(“%d%d”,&n,&m);
for(int i = 1;i <= n;++i)
{
scanf(“%d”, &cost[i]);
sum1 += cost[i];
if(cost[i] > maxm) maxm = cost[i];
}
long long l = maxm,r = sum1;
while(l < r)
{
long long mid = (l+r)/2,q = 0,sum = 0;
for(int i = 1;i <= n;++i)
{
if(sum + cost[i] <= mid) sum += cost[i];
else { q++; sum = cost[i]; }
}
if(sum) q++;
if(q <= m) r = mid;
else l = mid + 1;
}
printf(“%lld”, r);
}

猜你喜欢

转载自blog.csdn.net/L_NOVICE/article/details/82221448