luogu_P2678 跳石头

二分答案+贪心

#include<iostream> 
#include<cstdio>

#define ri register int
#define u int

namespace opt {
    
    inline u in() {
        u x(0),f(1);
        char s(getchar());
        while(s<'0'||s>'9') {
            if(s=='-') f=-1;
            s=getchar();
        }
        while(s>='0'&&s<='9') {
            x=(x<<1)+(x<<3)+s-'0';
            s=getchar();
        }
        return x*f;
    }
    
}

using opt::in;

#define NN 50005

#include<cstring>

namespace mainstay {
    
    u L,N,M,ans,a[NN],s[NN],t[NN];
    
    u check(const u &x){
        std::memcpy(t,s,sizeof(t));
        u cnt(0);
        for(ri i(1);i<=N+1;++i){
            if(t[i]<x) t[i+1]+=t[i],++cnt;
            if(cnt>M) return 0;
        }
        return 1;
    }
    
    inline void solve(){
        L=in(),N=in(),M=in();
        for(ri i(1);i<=N;++i) a[i]=in(),s[i]=a[i]-a[i-1];
        a[N+1]=L,s[N+1]=a[N+1]-a[N];
        u l(0),r(1000000000);
        while(l<=r){
            u mid(l+r>>1);
            if(check(mid)) l=mid+1,ans=mid;
            else r=mid-1;
        }
        std::cout<<ans;
    }
    
}

int main() {
    
    //freopen("x.txt","r",stdin);
    std::ios::sync_with_stdio(false);
    mainstay::solve();
    
}

猜你喜欢

转载自www.cnblogs.com/ling-zhi/p/11828744.html