poj 2456 求最大的最小值

poj 2456 求最大的最小值

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int n,m,l = 0,r = 0x7f7f7f7f,mid,ans;
bool check(int x){
    int f = a[0],cnt = 1;
    for(int i = 1;i < n; ++i){
        if(a[i] - f >= mid){
            f = a[i];
            cnt ++;
        }
    }
    return cnt >= m;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i = 0;i < n; ++i) scanf("%d",&a[i]);
    sort(a,a+n);
    while(l <= r){
        mid = l + (r-l)/2;
        if(check(mid)){
            l = mid + 1;
            ans = mid;
        }
        else r = mid - 1;
    }
    cout << ans;
    return 0;
}
原文地址:https://www.cnblogs.com/lukelmouse/p/11681437.html