結果

問題 No.2898 Update Max
ユーザー vjudge1
提出日時 2024-09-25 16:08:22
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,241 bytes
コンパイル時間 1,038 ms
コンパイル使用メモリ 88,856 KB
実行使用メモリ 13,884 KB
最終ジャッジ日時 2024-09-25 16:08:29
合計ジャッジ時間 6,957 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 16 WA * 9 TLE * 1 -- * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

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

bool vis[MAXN];
vector<int> se;
int n, m, 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;
    }
  }
  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() + 1;
      y = max(y, c + 1);
      for (int i = y; i <= m; i++) {
        ans = (ans + 1ll * A(i - 1, c) * fac[m - c - 1]) % Mod;
      }
      c++;
    }
    x = max(x, a[i]);
  }
  cout << ans;
  return 0;
}
0