結果

問題 No.1709 Indistinguishable by MEX
ユーザー planesplanes
提出日時 2021-10-16 00:25:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 261 ms / 2,000 ms
コード長 1,999 bytes
コンパイル時間 1,660 ms
コンパイル使用メモリ 173,104 KB
実行使用メモリ 19,236 KB
最終ジャッジ日時 2023-10-17 21:53:08
合計ジャッジ時間 6,906 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 211 ms
17,916 KB
testcase_09 AC 172 ms
16,860 KB
testcase_10 AC 157 ms
12,508 KB
testcase_11 AC 129 ms
11,452 KB
testcase_12 AC 125 ms
11,452 KB
testcase_13 AC 188 ms
17,388 KB
testcase_14 AC 172 ms
16,860 KB
testcase_15 AC 181 ms
17,388 KB
testcase_16 AC 233 ms
18,972 KB
testcase_17 AC 217 ms
18,444 KB
testcase_18 AC 260 ms
19,236 KB
testcase_19 AC 259 ms
19,236 KB
testcase_20 AC 261 ms
19,236 KB
testcase_21 AC 245 ms
19,236 KB
testcase_22 AC 244 ms
19,236 KB
testcase_23 AC 244 ms
19,236 KB
testcase_24 AC 244 ms
19,236 KB
testcase_25 AC 244 ms
19,236 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 133 ms
11,716 KB
testcase_28 AC 124 ms
11,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h> 
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()
 #define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)

ll mod=998244353;
ll mod_pow(ll x,ll n,ll mod) {
ll res=1;
  while(n>0) {
if(n&1) {
res=res*x%mod;
}
   x=x*x%mod;
    n>>=1;
  }
return res;
}



ll INF=2e18;
const ll MAX_N=1<<17;
ll n;
vector<pair<ll,ll>> dat;

void init(ll n_) {
    n=1;
    while (n<n_) n*=2;
dat=vector<pair<ll,ll>> (2*n-1);

}

    void update(ll k,ll i) {
        k+=n-1;
        dat[k]={k-n+1,i};

        while(k>0) {
            k=(k-1)/2;
            dat[k].first=dat[2*k+1].first;
            if(dat[2*k+2].second==0) dat[k].second=dat[2*k+1].second;
           else if(dat[2*k+1].second==dat[2*k+2].first) dat[k].second=dat[2*k+2].second;
           else dat[k].second=dat[2*k+1].second;
            }
        }
    



int main() {
    ll N;cin>>N;
    init(N);
    for(ll i=0;i<n;i++) {
        update(i,0);
    }
    
    vector<ll> P(N);  
  vector<ll> ind(N);
    for(ll i=0;i<N;i++) {
        cin>>P[i];
        ind[P[i]]=i;
    }
vector<ll> up(N+1);
up[0]=1;
for(ll i=1;i<=N;i++) {
    up[i]=up[i-1]*i;
    up[i]%=mod;
}

vector<ll> memo(N+1);
memo[0]=1;
for(ll i=1;i<=N;i++){
    memo[i]=mod_pow(i,mod-2,mod);
}
vector<ll> down(N+1);
down[0]=1;
for(ll i=1;i<=N;i++) {
    down[i]=down[i-1]*memo[i];
    down[i]%=mod;
}


    ll s=ind[0];
    ll g=ind[0];

    ll ans=1;
ll now=1;
update(0,1);
    while(now<N) {
        ll next;
        if(ind[now]<s) {
            for(ll i=ind[now];i<s;i++) {
                update(P[i],P[i]+1);
            }
            
            s=ind[now];
             next=dat[0].second;
        }
       else {
           for(ll i=g+1;i<=ind[now];i++) {
               update(P[i],P[i]+1);
           }
           g=ind[now];
            next=dat[0].second;
       }
       
ll k=up[g-s-now]%mod*down[g-s-(next-1)]%mod;
ans*=k%mod;
ans%=mod;
    now=next;
    }

    cout<<ans%mod<<endl;/**/

}

0