Convention
题面

题意
问用m辆公交车(每一辆公交车载人数量是一个定值)运送n头奶牛过程中,在把每一个奶牛都可以运走的情况下,每头奶牛的最少等待时间。
分析
What is the smallest possible value of the maximum waiting time是最关键的题眼,这是二分法使用的要求。
所以,我们就可以二分等待时间,而left与right的取值就是0和等待的最大时间。这个二分模板就不在此赘述,这道题最重要的代码是check函数代码。check函数的目的就是检测一下当前时间是不是可以再小。所以,我们需要判断两个东西。

  1. 车子够用(每次更新一辆车子开始上车的牛编号和最后上车的牛编号)
  2. 时间要求(如果出现时间还没结束但是车子已经满了,那么有些牛就必须等待下一辆车)

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll maxn=1e5+5;
ll n,m,c,a[maxn],st,bmax,slo;
int check(ll time){
st=a[1];
slo=1;
bmax=m-1;
for(ll i=2;i<=n;i++){
	if(a[i]-st>time||i-slo>=c){
		st=a[i];///the time of next cow
		slo=i;///the location of next cow
		bmax--;
		if(bmax<0) return 0;
	}
}
return 1;
}
int main(){
	ll left,right;
	cin>>n>>m>>c;
	for(ll i=1;i<=n;i++)
		cin>>a[i];
	sort(a+1,a+n+1);
	left=0;right=a[n]-a[1];
	while(left<right){
		ll mid=(right+left)/2;
		if(check(mid)) right=mid;
		else left=mid+1;
		/*printf("left=%d right=%d\n",left,right);*/
	}
	cout<<left<<endl;
    return 0;
}