2019_GDUT_ newborn Topics I anthology I HYSBZ - 1734

topic:

Farmer John built a long corral, which includes NN (2 <= N <= 100,000) compartments, these cubicles are numbered x1, ..., xN (0 < = xi <= 1,000,000,000). However, , John of C (2 <= C <= N) cows who do not like the layout, and a few head of cattle in a compartment, they will battle occurred. To keep cattle hurt each other. John decided to allocate its own compartment to cattle, the minimum distance between any two cows as large as possible, then, what is the largest minimum distance is it
Input
First line: two integers N and C space-separated
second row - N + 1 th line: i + 1 xi line indicates the position of
Output

The first line: an integer, the minimum value of the maximum

The Input the Sample
. 5 3
. 1
2
. 8
. 4
. 9
the Sample the Output
three
cattle in this minimum distance is 3, 4, 8

Method: sorting compartment to give a distance between the first compartment and the compartments furthest, then half this answer, if the current topic mid able to meet the conditions in the (mid, R & lt] to find the intermediate, If the letter does not go [l, mid) to find.

Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,c,a[100010];

int check(int x)
{
	int t=a[1],ans=1;
	for (int i=2;i<=n;i++)
	{
		if (a[i]-t>=x)
		{
			ans++;
			t=a[i];
		}
	}
	if (ans>=c) return 1;
	else return 0;
}

int main()
{
	cin>>n>>c;
	for (int i=1;i<=n;i++)
	scanf("%d",a+i);
	sort(a+1,a+1+n);
	int l=0,r=a[n]-a[1],mid,ans;
	while (l<=r)
	{
		mid=(l+r)/2;
		if (check(mid))
		{
			l=mid+1;
			ans=mid;
		}
		else
		{
			r=mid-1;
		}
	}
	cout<<ans;
}
Published 14 original articles · won praise 0 · Views 299

Guess you like

Origin blog.csdn.net/qq_39581539/article/details/103964676
I
"I"
I: