暑期集训3:几何基础 练习题H: POJ - 2456

2018学校暑期集训第三天——几何基础

练习题H  ——   POJ - 2456 

Aggressive cows


Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000).

His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?

Input

* Line 1: Two space-separated integers: N and C 

* Lines 2..N+1: Line i+1 contains an integer stall location, xi

Output

* Line 1: One integer: the largest minimum distance

Sample Input

5 3
1
2
8
4
9

Sample Output

3

Hint

OUTPUT DETAILS: 

FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3. 

Huge input data,scanf is recommended.


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;

int tree[100010];

bool judge(int mid, int n, int m)
{
	int a = 0;
	for(int i=1; i<m; i++){
		int b=a+1;
		while(b<n && tree[b]-tree[a]<mid)
			b++;
		if(b==n)
			return false;
		a = b;
	}
	return true;
}

int main(void)
{
	int n,m; 
	cin >> n >> m;
	for(int i=0;i<n;i++)
		scanf("%d", &tree[i]);
	
	sort(tree,tree+n);
	
	int min=0,max=99999999;
	while(max-min>1){
		int mid=(min+max)/2;
		if(judge(mid,n,m))
			min = mid;
		else
			max = mid;
	}
	
	cout << min << endl; 
	
	return 0;
}

 

全部评论

相关推荐

04-03 18:59
吉林大学 Java
大专人陈义:别投了,我看到有人点了第二个链接投递,还没退出界面,不合适的邮件就发过来了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务