码迷,mamicode.com
首页 > 其他好文 > 详细

二分-F - Aggressive cows

时间:2020-01-22 13:14:00      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:data   you   poj   detail   ble   tween   clu   nim   http   

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 }

 

二分-F - Aggressive cows

标签:data   you   poj   detail   ble   tween   clu   nim   http   

原文地址:https://www.cnblogs.com/0424lrn/p/12228280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!