E. Convention(二分)

http://codeforces.com/group/NVaJtLaLjS/contest/238203/problem/E

E. Convention

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Cows from all over the world are arriving at the local airport to attend the convention and eat grass. Specifically, there are NN cows arriving at the airport (1≤N≤1051≤N≤105) and cow ii arrives at time titi (0≤ti≤1090≤ti≤109). Farmer John has arranged MM (1≤M≤1051≤M≤105) buses to transport the cows from the airport. Each bus can hold up to CC cows in it (1≤C≤N1≤C≤N). Farmer John is waiting with the buses at the airport and would like to assign the arriving cows to the buses. A bus can leave at the time when the last cow on it arrives. Farmer John wants to be a good host and so does not want to keep the arriving cows waiting at the airport too long. What is the smallest possible value of the maximum waiting time of any one arriving cow if Farmer John coordinates his buses optimally? A cow's waiting time is the difference between her arrival time and the departure of her assigned bus.

It is guaranteed that MC≥NMC≥N.

Input

The first line contains three space separated integers NN, MM, and CC. The next line contains NN space separated integers representing the arrival time of each cow.

Output

Please write one line containing the optimal minimum maximum waiting time for any one arriving cow.

Example

input

Copy

6 3 2
1 1 10 14 4 3

output

Copy

4

题目大意:

给n头牛,m辆车,c车最多载牛数。再给n头牛的到达时间,等待时间=上车时间-到达时间;

车在任意一头牛到达后都可出发。求让牛最小的最大等待时间。

题目分析:

题中的这句话是关键:Please write one line containing the optimal minimum maximum waiting time for any one arriving cow.

最小的最大等待时间。暗示二分。二分解决最大化最小值等问题。

那么二分答案,在写个check 当车坐满人后或等待时间大于二分答案,则ans++;

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100005;
int n,m,c;int a[N];
int check(int x)
{
	int s=1;int last=0;
	for(int i=0;i<n;i++)
	{
		if(i-last>=c||a[i]-a[last]>x)
		{
			s++;
			last=i;
		}
	}
	return m>=s;
}
int main()
{
	cin>>n>>m>>c;
	for(int i=0;i<n;i++)cin>>a[i];
	sort(a,a+n);
	int l=0,r=a[n-1]-a[0],mid;
	while(l<=r)
	{
		mid=(l+r)>>1;
		if(check(mid))r=mid-1;
		else l=mid+1;
	}
	cout<<r+1<<endl;
} 

猜你喜欢

转载自blog.csdn.net/qq_43490894/article/details/87534301