結果

問題 No.3371 Add Insert Operations
コンテスト
ユーザー noya2
提出日時 2025-11-16 00:23:40
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,579 bytes
コンパイル時間 3,262 ms
コンパイル使用メモリ 294,284 KB
実行使用メモリ 8,864 KB
最終ジャッジ日時 2025-11-17 20:47:32
合計ジャッジ時間 5,093 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10 WA * 24 RE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,s,n) for (int i = (int)(s); i < (int)(n); i++)
#define reb(i,s,n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) begin(v), end(v)
using namespace std;
using ll = long long;
bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }

template<typename T>
ostream &operator<<(ostream &os, const vector<T> &a){
    if (a.empty()) return os;
    os << a.front();
    for (auto e : a | views::drop(1)){
        os << ' ' << e;
    }
    return os;
}

void dump(auto ...vs){
    ((cout << vs << ' '), ...) << endl;
}

#include<atcoder/modint>
using mint = atcoder::modint998244353;

void solve(){
    int n; cin >> n;
    vector<int> a(n);
    rep(i,0,n) cin >> a[i];
    vector<int> ord; ord.reserve(n);
    vector<pair<int,int>> pars(n);
    {
        vector<int> prv(n), nxt(n);
        rep(i,0,n){
            prv[i] = i-1;
            nxt[i] = i+1;
        }
        queue<int> que;
        rep(i,0,n){
            if (a[i] == 0){
                que.emplace(i);
            }
        }
        while (!que.empty()){
            int v = que.front(); que.pop();
            ord.emplace_back(v);
            int pv = prv[v], nv = nxt[v];
            pars[v] = {pv,nv};
            if (pv != -1){
                if (a[pv] == 0){
                    break;
                }
                if (--a[pv] == 0){
                    que.emplace(pv);
                }
                nxt[pv] = nv;
            }
            if (nv != -1){
                if (a[nv] == 0){
                    break;
                }
                if (--a[nv] == 0){
                    que.emplace(nv);
                }
                prv[nv] = pv;
            }
        }
        if (ssize(ord) != n){
            cout << 0 << '\n';
            return ;
        }
    }
    vector<int> inv(n);
    rep(i,0,n){
        inv[ord[i]] = i;
    }
    mint ans = 1, div = 1;
    vector<int> sub(n,1);
    int root = ord.back();
    for (int v : ord){
        ans *= v+1;
        div *= sub[v];
        if (v == root) break;
        auto [l, r] = pars[v];
        if (l == -1){
            assert(r != n);
            sub[r] += sub[v];
        }
        else if (r == n){
            assert(l != -1);
            sub[l] += sub[v];
        }
        else {
            int p = (inv[l] < inv[r] ? l : r);
            sub[p] += sub[v];
        }
    }
    dump(sub);
    dump(ord);
    ans /= div;
    cout << ans.val() << '\n';
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    solve();
}
0