二分-F - Aggressive cows

F - 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头牛安置在这些牛棚中,使得每头的最小坐标间隔最大。
 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 typedef long long ll;
 6 int n,c;
 7 ll a[100005];
 8 
 9 int solve(ll x){
10     int cnt = 1,drr=a[0];
11     for(int i=1; i<n; i++){
12         if(a[i] - drr >= x){
13             cnt++;
14             drr = a[i];
15         }
16         if(cnt >= c)    return 1;
17     }
18     return 0;
19 }
20 int main(){
21     scanf("%d %d", &n, &c);
22     for(int i=0; i<n; i++)
23         scanf("%d", a+i);
24     sort(a, a+n);
25     
26     ll left = 0,right = 1000000005;    
27     ll mid;
28     while(right - left > 1){
29         mid = (left + right)/2;
30         if(solve(mid))    left = mid;
31         else            right = mid;
32     }
33     printf("%lld", left);
34 }

 

上一篇:mfc 注册表


下一篇:Aggressive cows(二分)