Luogu P2678 跳石头

题目链接:Click here

Solution:

最小值最大,显然二分,二分出mid后贪心去除石头,判断m次内是否可行即可

Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+11;
int n,m,k,a[N],b[N];
int check(int mid){
    int tim=m;b[n+1]=k-a[n];
    for(int i=1;i<=n;i++) b[i]=a[i]-a[i-1];
    for(int i=1;i<=n+1;i++){
        if(b[i]>=mid) continue;
        if(!tim) return 0;--tim;
        b[i+1]+=b[i],b[i]=b[i-1];
    }return 1;
}
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
signed main(){
    k=read(),n=read(),m=read();
    for(int i=1;i<=n;i++)
        a[i]=read(),b[i]=a[i]-a[i-1];
    b[n+1]=k-a[n];
    int l=1,r=k,re=-1;
    while(l<=r){
        int mid=l+r>>1;
        if(check(mid)) re=mid,l=mid+1;
        else r=mid-1;
    }printf("%lld\n",re);
    return 0;
}
上一篇:C++雾中风景2:struct还是class?


下一篇:【【洛谷P2678 跳石头】——%%%ShawnZhou大佬】