結果

問題 No.2898 Update Max
ユーザー vjudge1
提出日時 2024-09-25 12:14:07
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 1,199 bytes
コンパイル時間 3,790 ms
コンパイル使用メモリ 275,876 KB
実行使用メモリ 7,496 KB
最終ジャッジ日時 2024-09-25 12:14:15
合計ジャッジ時間 5,719 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;
const int N = 2e5 + 10, P = 998244353;

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

int inv[N], fact[N], invFact[N];

int A(int n, int m) {
    if(m < 0 || m > n) return 0;
    return fact[n] * (i64)invFact[n - m] % P;
}

int main() {
    inv[1] = fact[0] = fact[1] = invFact[0] = invFact[1] = 1;
    for(int i = 2; i < N; i ++) {
        inv[i] = (P - P / i) * (i64)inv[P % i] % P;
        fact[i] = fact[i - 1] * (i64)i % P;
        invFact[i] = invFact[i - 1] * (i64)inv[i] % P;
    }

    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    for(int i = 1; i <= n; i ++)
        cin >> a[i];

    for(int i = 1; i <= n; i ++) {
        if(a[i] == -1) m ++;
        else b[a[i]] ++;
    }

    for(int i = 1; i <= n; i ++)
        b[i] = b[i - 1] + (1 - b[i]);

    int ans = 0;
    for(int i = 1, p = 0, q = 0; i <= n; i ++) {
        if(a[i] == -1) {
            q ++;
            ans = (ans + (fact[m] + P - A(b[p], q) * (i64)fact[m - q] % P) * inv[q]) % P;
        }else{
            if(a[i] < p) continue;
            p = a[i];
            ans = (ans + A(b[p], q) * (i64)fact[m - q]) % P;
        }
    }
    cout << ans << "\n";
}
0