P1824 [USACO05FEB] 进击的奶牛 Aggressive Cows G

点击查看代码
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N=1e5+10;
LL x[N];
int n,m;

bool check(int d)
{
    int cnt=1;
    LL last=x[1];
    for(int i=2;i<=n;i++){
        if(x[i]-last>=d){
            last=x[i];
            cnt++;
        }
    }

    return cnt>=m;
}

int main()
{
    ios::sync_with_stdio(0),cin.tie(0);

    cin>>n>>m;

    for(int i=1;i<=n;i++) cin>>x[i];

    sort(x+1,x+1+n);

    int ans=0;
    int l=1,r=x[n]-x[1];

    while(l<=r){
        int mid=l+(r-l)/2;
        if(check(mid)){
            ans=mid;
            l=mid+1;
        }else{
            r=mid-1;
        }
    }

    cout<<ans<<endl;
}



posted @ 2026-01-28 20:03  AnoSky  阅读(12)  评论(0)    收藏  举报