Loading

Team Weekly Contest 2022-10-22

The 2021 CCPC Guangzhou Onsite

C. Necklace

二分

#include <bits/stdc++.h>

using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
#define rep(a, b, c) for(int (a)=(b);(a)<=(c);(a)++)
#define per(a, b, c) for(int (a)=(b);(a)>=(c);(a)--)
#define mset(var, val) memset(var,val,sizeof(var))
#define ll long long
#define int ll
#define fi first
#define se second
#define no "NO\n"
#define yes "YES\n"
#define pb push_back
#define endl "\n"
#define pii pair<int,int>
#define pll pair<ll,ll>
const int N = 1e6 + 5;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);

int n,m;
int a[N],dis[N];

inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}

bool check(int x){
    int minn=dis[1];
    int now=0,cnt=0;
    rep(i,2,m){
        int tx=x-now;
        if(tx<=0)   return false;
        if(dis[i]>tx){
            now=dis[i]-tx;
            minn=min(minn,tx-1);
        }else{
            now=0;
            cnt+=min(minn,tx-dis[i]);
            minn=min(minn-min(minn,tx-dis[i]),dis[i]-1);
        }
    }
    int ans=now+dis[1]+1-x;
    if(x-now>=dis[1]+1 || cnt>=ans) return true;
    return false;
}

void solve() {
    n=read();
    m=read();
    rep(i,1,m){
        a[i] = read();
    }
    rep(i,2,m){
        dis[i]=a[i]-a[i-1];
    }
    dis[1]=n-a[m]+a[1]-1;
    ll r=n,l=1,ans=n;
    while(l<=r){
        int mid=(l+r)>>1;
        if(check(mid)){
            r=mid-1;
            ans=min(ans,mid);
        }else{
            l=mid+1;
        }
    }
    printf("%lld\n",ans);
}

signed main() {
//    IOS;
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
posted @ 2022-10-31 18:55  ShG1211  阅读(38)  评论(0)    收藏  举报