题目描述
一场别开生面的牛吃草大会就要在 Farmer John 的农场举办了!
世界各地的奶牛将会到达当地的机场,前来参会并且吃草。具体地说,有N头奶牛到达了机场(1≤N≤105),其中奶牛i在时间ti(0≤ti≤109)到达。Farmer John 安排了M(1≤M≤105)辆大巴来机场接这些奶牛。每辆大巴可以乘坐C头奶牛(1≤C≤N)。
Farmer John 正在机场等待奶牛们到来,并且准备安排到达的奶牛们乘坐大巴。当最后一头乘坐某辆大巴的奶牛到达的时候,这辆大巴就可以发车了。
Farmer John 想要做一个优秀的主办者,所以并不想让奶牛们在机场等待过长的时间。如果 Farmer John 合理地协调这些大巴,等待时间最长的奶牛等待的时间的最小值是多少?一头奶牛的等待时间等于她的到达时间与她乘坐的大巴的发车时间之差。
世界各地的奶牛将会到达当地的机场,前来参会并且吃草。具体地说,有N头奶牛到达了机场(1≤N≤105),其中奶牛i在时间ti(0≤ti≤109)到达。Farmer John 安排了M(1≤M≤105)辆大巴来机场接这些奶牛。每辆大巴可以乘坐C头奶牛(1≤C≤N)。
Farmer John 正在机场等待奶牛们到来,并且准备安排到达的奶牛们乘坐大巴。当最后一头乘坐某辆大巴的奶牛到达的时候,这辆大巴就可以发车了。
Farmer John 想要做一个优秀的主办者,所以并不想让奶牛们在机场等待过长的时间。如果 Farmer John 合理地协调这些大巴,等待时间最长的奶牛等待的时间的最小值是多少?一头奶牛的等待时间等于她的到达时间与她乘坐的大巴的发车时间之差。
输入
输入的第一行包含三个空格分隔的整数N,M和C。
第二行包含N空格分隔的整数,表示每头奶牛到达的时间。
第二行包含N空格分隔的整数,表示每头奶牛到达的时间。
输出
输出一行,包含所有到达的奶牛中的最大等待时间的最小值。
样例输入 Copy
6 3 2
1 1 10 14 4 3
样例输出 Copy
4
提示
样例解释
如果两头时间1到达的奶牛乘坐一辆巴士,时间2和时间4到达的奶牛乘坐乘坐第二辆,时间10和时间14到达的奶牛乘坐第三辆,那么等待时间最长的奶牛等待了4个单位时间(时间10到达的奶牛从时间10等到了时间14)。
如果两头时间1到达的奶牛乘坐一辆巴士,时间2和时间4到达的奶牛乘坐乘坐第二辆,时间10和时间14到达的奶牛乘坐第三辆,那么等待时间最长的奶牛等待了4个单位时间(时间10到达的奶牛从时间10等到了时间14)。
这个题和跳石头那个题差不多
#include<iostream> #include<algorithm> #include<cstring> using namespace std; const int maxn=1e6+100; int n,m,c; int a[maxn]; int judge(int x){ int ans=1; int cnt=0; int p=1; for(int i=1;i<=n;i++){ if((a[i]-a[p])<=x&&cnt+1<=c){ cnt++; } else{ ans++; cnt=1; p=i; } } if(ans<=m){ return 1; } else{ return 0; } } int main(){ cin>>n>>m>>c; for(int i=1;i<=n;i++){ cin>>a[i]; } sort(a+1,a+n+1); int l=0,r=1e9; int ans=0; while(r>=l){ int mid=(l+r)/2; if(judge(mid)){ r=mid-1; ans=mid; } else{ l=mid+1; } } cout<<ans<<endl; }