LOADING

加载过慢请开启缓存,浏览器默认开启

daily 2

2023/6/21 daily

CF 883I

int main(){
    int n,k;
    cin>>n>>k;
    vector<int>a(n+1);
    for(int i=1;i<=n;++i){
        cin>>a[i];
    }
    sort(a.begin()+1,a.end());
    vector<int>dp(n+1);
    dp[0]=1;
    function<bool(int)>check=[&](int v){
        int l=1,r;
        for(int i=1;i<=n;++i){
            dp[i]=0;
            while(a[i]-a[l]>v)++l;
            r=i-k+1;
            for(int j=l;j<=r;++j,++l){
                if(dp[j-1]){
                    dp[i]=1;
                    break;
                }
            }
        }
        return dp[n]==1;
    };
    int l=0,r=a.back()-a[1],ans=0;
    while(l<=r){
        int m=(l+r)>>1;
        if(check(m)){
            ans=m;
            r=m-1;
        }else{
            l=m+1;
        }
    }
    cout<<ans<<'\n';
}

CF 847E

int main(){
    int n;
    cin>>n;
    string s;
    cin>>s;
    vector<int>pro,peo;
    for(int i=0;i<s.size();++i){
        if(s[i]=='*'){
            pro.push_back(i);
        }
        if(s[i]=='P'){
            peo.push_back(i);
        }
    }
    function<int(int,int,int)>cal=[&](int l,int r,int cur){
        return min(abs(r-cur),abs(cur-l))+abs(r-l);
    };
    function<bool(int)>check=[&](int time){
        int pre=0,cur=0;
        for(int p:peo){
            while(cur<pro.size()&&cal(pro[pre],pro[cur],p)<=time)++cur;
            pre=cur;
        }
        return cur==pro.size();
    };
    int l=0,r=2*n,ans=-1;
    while(l<=r){
        int m=(l+r)>>1;
        if(check(m)){
            ans=m;
            r=m-1;
        }else{
            l=m+1;
        }
    }
    cout<<ans<<'\n';
}