結果

問題 No.2898 Update Max
ユーザー vjudge1vjudge1
提出日時 2024-09-25 20:54:06
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 1,277 bytes
コンパイル時間 1,486 ms
コンパイル使用メモリ 167,368 KB
実行使用メモリ 30,080 KB
最終ジャッジ日時 2024-09-25 20:54:13
合計ジャッジ時間 4,608 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 1e6 + 5, mod = 998244353;

int n, x[N], ans, sum, cnt[N], now;

int inv[N], g[N], f[N];

bool vis[N];

int C(int n, int m) {
  if (n < m) {
    return 0;
  }
  return f[n] * g[m] % mod * g[n - m] % mod;
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  inv[1] = f[0] = g[0] = 1;
  for (int i = 1; i <= 1000000; i++) {
    f[i] = f[i - 1] * i % mod;
    if (i > 1) inv[i] = inv[mod % i] * (mod - mod / i) % mod;
    g[i] = g[i - 1] * inv[i] % mod;
  }
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
    cnt[max(0ll, x[i])] = 1;
    now += (x[i] == -1);
  }
  cnt[0] = 0;
  for (int i = 1; i <= n; i++) {
    cnt[i] = !cnt[i];
    cnt[i] += cnt[i - 1];
  }
  int maxi = 0, c = 0;
  for (int i = 1; i <= n; i++) {
    maxi = max(maxi, x[i]);
    if (x[i] == -1) {
      c++;
      ans += (C(now, c) - C(cnt[maxi], c) + mod) % mod * f[c - 1] % mod * f[now - c] % mod;
      ans %= mod;
    }
    else {
      if (maxi == x[i] && cnt[maxi] >= c) {
        ans += C(cnt[maxi], c) * f[c] % mod * f[now - c] % mod;
        ans %= mod;
      }
    }
  }
  cout << ans << "\n";
  return 0;
}

/*
4
1 -1 3 -1
6
2 4 5 3 6 1
17
-1 -1 8 2 10 9 12 -1 7 -1 -1 -1 11 -1 13 -1 -1
*/
0