結果
| 問題 |
No.1709 Indistinguishable by MEX
|
| コンテスト | |
| ユーザー |
planes
|
| 提出日時 | 2021-10-16 00:25:39 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 244 ms / 2,000 ms |
| コード長 | 1,999 bytes |
| コンパイル時間 | 1,390 ms |
| コンパイル使用メモリ | 173,908 KB |
| 実行使用メモリ | 19,300 KB |
| 最終ジャッジ日時 | 2024-09-17 19:07:36 |
| 合計ジャッジ時間 | 6,060 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
ソースコード
#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;/**/
}
planes