【luogu3853】路標設置(二分,模擬)

題目大意

把公路上相鄰路標的最大距離定義爲該公路的“空曠指數”,給出個路標距起點的距離,求最小“空曠指數”值
簡單二分+模擬即可
#include<iostream>
using namespace std;
const int maxn=100010;
int L,n,k;
int a[maxn]; 

int pd (int tmp){                    //tmp爲枚舉的最小的最大距離
    int now=0,t=0;                //t爲當前所設路標數
    for(int i=1;i<=n;i++){
        while(a[i]-now>tmp)           //now爲當前路標位置
            now+=tmp,t++;  
            if(t>k) break; 
            if(now<a[i]) now=a[i];
        }
    while(L-now>tmp)
        now+=tmp,t++;           //勿忘終點  
    return t<=k;     
}
int main(){
    cin>>L>>n>>k;
    for (int i=1;i<=n;i++){
        cin>>a[i];
    }   
    int l=0,r=L;
    int ans=0;
    while (l<=r){
        int mid=l+(r-l)/2;
        if (pd(mid)) {
                ans=mid; 
                r=mid-1;
            } 
            else l=mid+1;
    }        //熟悉的配方
    cout<<ans;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章