Aggressive cows

直接上中文了

Descriptions

农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000,000,000). 但是,John的X (2 <= X <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗。为了不让牛互相伤害。John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢?Input有多组测试数据,以EOF结束。 第一行:空格分隔的两个整数N和X 第二行——第N+1行:分别指出了xi的位置Output每组测试数据输出一个整数,满足题意的最大的最小值,注意换行。

Sample Input

Sample Output

Hint

位置放一头牛,4位置放一头牛,它们的差值为3;最后一头牛放在8或9位置都可以,和4位置的差值分别为4、5,和1位置的差值分别为7和8,不比3小,所以最大的最小值为3。

题目链接

https://vjudge.net/problem/POJ-2456

简单二分,枚举一下间距即可,直接看代码吧

AC代码

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 100000+5
using namespace std;
int l,r;//左,右
int n,x;
int a[Maxn];//存牛栏
int judge(int k)//判断间距k,看能否能放置任意两头牛
{
int pos=a[];
int cnt=;//表示放进了cnt头牛
for(int i=; i<n; i++)
{
if(a[i]-pos>=k)//牛之间的距离大于等于k即可放入牛栏
{
pos=a[i];
cnt++;
}
if(cnt>=x)//牛的数量够了
return ;
}
return ;
}
int main()
{
while(cin>>n>>x)
{
for(int i=; i<n; i++)
cin>>a[i];
sort(a,a+n);
l=,r=a[n-]-a[];//最小距离为1,最大距离为牛栏编号最大的减去编号最小的
int ans=;//答案
while(l<r)
{
int mid=(l+r)/;
if(judge(mid))
{
l=mid+;
ans=mid;
}
else
r=mid;
}
cout<<ans<<endl;
}
return ;
}
05-15 04:28