POJ-2456 (binary greedy +)

The meaning of problems: 
    there are n Kraal, selected from the cow into the m, with n corresponding to points on a line segment m points selected, 
such that the minimum distance value between adjacent maximum points

Thinking: Piggy + binary
bipartite enumeration bovine two adjacent spacing, the greater than or equal to this pitch is determined whether all into cattle.

#include<iostream>
#include<algorithm>
#include<cstdio>
#define max 100005
using namespace std;
int n,c,a[max];
bool judge(int d) {
    int index = 0, num=1;
    for(int i=1;i<n;i++){
        if(a[i]-a[index]>=d){
            index = i;
            num++;
        } 
        if(num>=c) return true;
    } 
    return false;
}
int main() {

    while(scanf("%d%d",&n,&c)!=EOF) {
        for(int i=0; i<n; i++) {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        int l,r,ans;
        l = 1; r = a[n-1]-a[0]; ans = 0;
        while(l<=r) {
            int mid = (l+r)>>1;
            if(judge(mid)) {
                ans = mid;
                l = mid+1;
            } else {
                r = mid -1;
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Lemon1234/p/11607132.html