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.

有N个隔断和C头牛,牛希望彼此的距离尽可能远。用二分法做。

源代码:

Select Code

#include<iostream>
#include<algorithm>

using namespace std;

int a[100005];

int main()
{
	int N,C;
	cin>>N>>C;
	for(int i=0;i<N;i++)
		cin>>a[i];
	sort(a,a+N);
	int low=1,hign=a[N-1]-a[0],mid,s=1,x,n=1;
	//cout<<low<<hign<<endl;
	while(hign-low>1)
	{
		s=1;
		n=1;
		mid=(low+hign)/2;
		x=a[0]+mid;
		while(x<=a[N-1])
		{
			if(a[n++]>=x)
			{
				s++;
				x=mid+a[n-1];
			//	cout<<n-1<<endl;
			}
		}
		if(s>=C)
			low=mid;
		if(s<C)
			hign=mid;
		//cout<<s<<" "<<mid<<endl;
		//cout<<endl;
	}
	if(s>=C)
		cout<<mid<<endl;
		else
			cout<<mid-1<<endl;

	return 0;
}
时间: 2024-09-04 11:41:58

POJ - 2456 :Aggressive cows的相关文章

【POJ - 2456】Aggressive cows(二分)

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

POJ 2186:Popular Cows(强连通分量)

[题目链接] http://poj.org/problem?id=2186 [题目大意] 给出一张有向图,问能被所有点到达的点的数量 [题解] 我们发现能成为答案的,只有拓扑序最后的SCC中的所有点, 那么我们从其中一个点开始沿反图dfs,如果能访问到全图, 则答案为其所在SCC的大小,否则为0. [代码] #include <cstdio> #include <algorithm> #include <vector> #include <cstring>

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

题目传送门 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)

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 【二分】

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

[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(二分搜索最大化最小值)

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,...