题解:CF1392F Omkar and Landslide

· · 题解

手玩一下,我们发现最终的情况一定是一个 L,L+1,L+2,...,R 的阶梯中间插入一个与上一级阶梯登高的阶梯,并且每次往后放入一座山时可以视作高度 R+1 以上的山体会向前塌,具体而言这会使得那级与上一级登高的阶梯后移,于是考虑利用周期性快速算下即可。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e6+114;
int a[maxn],n;
int d,pos,h;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    d=h=a[1];
    for(int i=2;i<=n;i++){
        h++;
        int c=a[i]-h;
        int t=i-1-pos;
        if(c==0) continue;
        if(c<=t){
            if(pos==0) d++;
            pos+=c;
        }else{
            if(pos==0) d++;
            c-=(t+1);
            pos=0;
            h++;
            int f=c/i;
            c%=i;
            d+=f;
            h+=f;
            if(c>0){
                d++;
                pos=c;
            }
        }
        /*
        for(int j=1;j<=c;j++){
            if(pos==0){
                pos=1;
                d++;
            }else if(pos+1<i){
                pos++;
            }else{
                pos=0;
                h++;
            }
        }*/
        //cout<<d<<' '<<h<<' '<<pos<<'\n';
    }
    cout<<d<<" ";
    for(int i=2;i<=n;i++){
        cout<<d+i-1-(pos==0?0:1)*(pos+1<=i?1:0)<<' ';
    }
    return 0;
}