POJ 2456 Aggressive cows(二分搜索最大化最小值)

Aggressive cows

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6372   Accepted: 3181

Description

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

题意:

有N个牛舍,第i个在Xi位置,同时有M头牛,将这M头牛放于牛舍中,使每头牛之间距离最大化。

思路:

因为无法直接确定每头牛的距离,我们可以考虑找到所有牛之间距离的最小值,利用二分从大到小搜索知道出现满足crt<N&&X[crt]-X[last]>d,此时的X[crt]-X[last]表示第crt个牛舍

与上一个可放置牛的牛舍距离,若大于d,则说明该crt或crt-1位置的牛舍处于临界点,可放置牛,若小于d则表示未达临界点,crt++,继续搜索。

代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
const long MAXN=100000;
const long INF=1000000000;
long X[MAXN];
long N,M;
int Tdfs(long d)
{
	long last=0;
	for(int i=1;i<M;i++){
		long crt=last+1;
		while(crt<N&&X[crt]-X[last]<d){
			crt++;
		}
		if(crt==N)return 0;
		last=crt;
	}
	return 1;
}
int main()
{
	int i;
	long lb,ub,mid;
	while(cin>>N>>M)
	{
		lb=0,ub=INF;
		for(i=0;i<N;i++)
			cin>>X[i];
		sort(X,X+N);
		while(ub-lb>1){
			mid=(lb+ub)/2;
			if(Tdfs(mid))
				lb=mid;
			else
				ub=mid;
		}
		cout<<lb<<endl;
	}
	return 0;
}

本题不需使用long long或者_int64,long数据类型足以,long最大支持21亿+。另外注意失败条件“if(crt==N)return 0;”,当返回1时,lb=mid增大d值,反之减小d值。

时间: 2024-10-07 10:08:05

POJ 2456 Aggressive cows(二分搜索最大化最小值)的相关文章

poj 2456 Aggressive cows(二分搜索之最大化最小值)

Description 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 an

poj 2456 Aggressive cows,二分,最大化最小值

描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000,000,000). 但是,John的C (2 <= C <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗.为了不让牛互相伤害.John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢? 输入 有多组测试数据,以EOF结束. 第

二分搜索 POJ 2456 Aggressive cows

题目传送门 1 /* 2 二分搜索:搜索安排最近牛的距离不小于d 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cmath> 7 using namespace std; 8 9 const int MAXN = 1e5 + 10; 10 const int INF = 0x3f3f3f3f; 11 int x[MAXN]; 12 int n, m; 13 14 bool check(int d)

[ACM] poj 2456 Aggressive cows (二分查找)

Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 Description 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,...

poj 2456 Aggressive cows(二分)

// n点中选c点放下c头牛 是的n-1段距离中的最短距离最大 ,求这个最大的最短距离 //假设当前的最小值为x,如果判断出最小差值为x时可以放下C头牛, //就先让x变大再判断:如果放不下,说明当前的x太大了, //就先让x变小然后再进行判断.直到求出一个最大的x就是最终的答案. # include <algorithm> # include <string.h> # include <stdio.h> using namespace std; int n,c,i,a

【贪心专题】POJ 2456 Aggressive cows &amp;&amp; NYOJ 586 疯牛(最大化最小值 贪心+二分搜索)

链接: click here~~ 题意:农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000,000,000). 但是,John的C (2 <= C <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗.为了不让牛互相伤害.John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢? [解题

POJ 2456 Aggressive cows 【二分】

Aggressive cows 题目链接:http://poj.org/problem?id=2456 题意:有N个位置,(2 <= N <= 100,000),要在这N个位置放入C头牛(2 <= C <= N),要是牛与牛之间的最小距离最大,求这个最大可能的最小距离. 分析:显然又是一个求最大化最小值的问题,很容易找到这个题的单调性,设这个最大可能的最小距离为Ans,Ans∈(0,(Pos[N-1]-Pos[0])/(C-1) );首先对N个位置进行排序,然后在区间(0,(Pos

POJ 2456 Aggressive cows (二分 基础)

Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7924   Accepted: 3959 Description 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,...

POJ 2456 Aggressive cows(二分)

题目链接:http://poj.org/problem?id=2456 题意:有一排n个牛舍,坐标分别为xi,有m头牛,希望尽可能把他们之间分开,求他们之间最近的两头牛之间的距离最大可以拉到多少.这是二分中最大化最小值的题目,英文的题目又最大又最小有的人不易理解. 思路:显然两头最近的牛的距离太大将没有一种方式安置在牛舍中,两头最近的牛距离越小就越能放置在牛舍中,所以用把答案二分出来,每个mid要模拟放置暴力下可不可以放得下. //532K 188MS #include<cstdio> #in