結果

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

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

const int MAXN = 2e5 + 5, Mod = 998244353;

bool vis[MAXN];
vector<int> se;
int n, m, k, ans, a[MAXN], b[MAXN], fac[MAXN] = {1, 1}, inv[MAXN] = {0, 1}, facinv[MAXN] = {1, 1};

int A(int n, int m) {
  if (n < m) return 0;
  return 1ll * fac[n] * facinv[n - m] % Mod;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] > 0) {
      vis[a[i]] = 1;
    }
  }
  if (!vis[1]) se.push_back(1);
  for (int i = 2; i <= n; i++) {
    fac[i] = 1ll * fac[i - 1] * i % Mod;
    inv[i] = 1ll * (Mod - Mod / i) * inv[Mod % i] % Mod;
    facinv[i] = 1ll * facinv[i - 1] * inv[i] % Mod;
    if (!vis[i]) se.push_back(i);
  }
  m = se.size();
  for (int i = 1, x = 0, c = 0; i <= n; i++) {
    if (a[i] > 0) {
      if (a[i] > x) {
        int y = lower_bound(se.begin(), se.end(), a[i]) - se.begin();
        ans = (ans + 1ll * A(y, c) * fac[m - c]) % Mod;
      }
    } else {
      int y = lower_bound(se.begin(), se.end(), x) - se.begin();
      c++, ans = (ans + 1ll * (A(m, c) - A(y, c) + Mod) * inv[c] % Mod * fac[m - c]) % Mod;
    }
    x = max(x, a[i]);
  }
  cout << ans;
  return 0;
}
0