1 solutions

  • 0
    @ 2025-3-3 16:21:27

    C++ :

    #include<bits/stdc++.h>
    
    using namespace std;
    int i,L,N,M,a[50010];
    
    int check(int mid){//检验函数,求出给定距离dist后 需要移走多少石头,是否不超过M 
        int i,last=0,cnt=0;
        for(i=1;i<=N;i++){
            if(a[i]-last<mid)cnt++;
            else last=a[i];
        }
        if(L-last<mid)cnt++;
        return cnt<=M;
    }
    
    int main(){
        scanf("%d %d %d",&L,&N,&M);
        for(i=1;i<=N;i++)scanf("%d",a+i);
        sort(a+1,a+1+N);
        int lo=1,hi=L,mid;
        while(lo<=hi){//二分查找模板 
            mid=(lo+hi)/2;
            if(check(mid))lo=mid+1;
            else hi=mid-1;
        }
        printf("%d\n",lo-1);
    }
     
    
    
    • 1

    Information

    ID
    9951
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By