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 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. Source |
[Submit] [Go Back] [Status] [Discuss]
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI acos(-1.0)
#define E exp(1)
using namespace std;
int a[maxn];
int n,m;
bool checked(int x){
int t=a[0];
int cnt=1;
FOR(i,1,n-1)
if(a[i]-t>=x){
t=a[i];
cnt++;
}
return cnt>=m;
}
int main(){
while(rd2(n,m)!=EOF){
FOR(i,0,n-1)
rd(a[i]);
sort(a,a+n);
int low,high,mid,ff;
low=0;
high=a[n-1];
while(low<=high){
mid=(low+high)/2;
if(checked(mid)){
ff=mid;
low=mid+1;
}
else high=mid-1;
}
printf("%d\n",ff);
}
return 0;
}
/*
5 3
1
2
8
4
9
*/