POJ —— 二分

来源:互联网 发布:3d场景软件 编辑:程序博客网 时间:2024/05/20 15:41
Aggressive cows
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4898 Accepted: 2415

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 312849

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

USACO 2005 February Gold

题意是有n间牛棚,在a数组中告诉你他们的位置,有M头牛,你要把他们放得尽量远,输出最小间隔的数量

思路就是我去对最小距离d二分。calc(d)表示安排牛的位置使得最近的两头牛都小于d,也就是所有的两牛间距小于d。

#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <cstring>#include <map>#include <string>#include <stack>#include <cctype>#include <vector>#include <queue>#include <set>#include <utility>#include <cassert>using namespace std;///#define Online_Judge#define outstars cout << "***********************" << endl;#define clr(a,b) memset(a,b,sizeof(a))#define lson l , mid  , rt << 1#define rson mid + 1 , r , rt << 1 | 1#define mk make_pair#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)const int MAXN = 100000 + 5;const int MAXS = 10000 + 50;const int sigma_size = 26;const long long LLMAX = 0x7fffffffffffffffLL;const long long LLMIN = 0x8000000000000000LL;const int INF = 0x7fffffff;const int IMIN = 0x80000000;const int inf = 1 << 30;#define eps 1e-8const long long MOD = 1000000000 + 7;const int mod = 100000;typedef long long LL;const double PI = acos(-1.0);typedef double D;typedef pair<int , int> pii;#define Bug(s) cout << "s = " << s << endl;///#pragma comment(linker, "/STACK:102400000,102400000")int n , m ,a[MAXN];bool calc(int d){    int las = 0;    for(int i = 1 ; i < m ; i++)    {        int cur = las + 1;        while(cur < n && a[cur] - a[las] < d)        {            cur++;        }        if(cur == n)return false;        las = cur;    }    return true;}void solve(){    sort(a , a + n);    int L = 0 , R = inf;    while(R - L > 1)    {        int mid = (L + R) >> 1;        if(calc(mid))L = mid;        else R = mid;    }    printf("%d\n" , L);}int main(){    while(~scanf("%d%d" , &n , &m))    {        for(int i = 0  ; i < n ; i++)        {            scanf("%d" , &a[i]);        }        solve();    }    return 0;}


原创粉丝点击