結果
問題 |
No.3052 Increasing Sliding Window Minimum
|
ユーザー |
|
提出日時 | 2025-02-03 23:42:31 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 214 ms / 2,000 ms |
コード長 | 2,066 bytes |
コンパイル時間 | 15,838 ms |
コンパイル使用メモリ | 357,740 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2025-02-03 23:42:52 |
合計ジャッジ時間 | 19,015 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 42 |
ソースコード
#include <bits/stdc++.h> #include "atcoder/modint.hpp" using mint = atcoder::modint998244353; #include "testlib.h" void solve() { int n = inf.readInt(2, 5000); inf.readEoln(); std::vector<int> A = inf.readInts(n, -1, n); inf.readEoln(); std::reverse(A.begin(), A.end()); std::vector<bool> rem(n, false); int cc = 0; for (int i = 0; i < n; i++) { if (A[i] == -1) continue; assert(A[i] != 0); A[i]--; ensuref(!rem[A[i]], "A[%d] is already used", i); rem[A[i]] = true; cc++; } std::vector<mint> dp1(n, 0), dp2(n, 0); if (A[0] == -1) { for (int i = 0; i < n; i++) { if (!rem[i]) dp1[i] = 1; } } else { dp1[A[0]] = 1; cc--; rem[A[0]] = false; } for (int i = 1; i < n; i++) { std::vector<mint> ndp1(n, 0), ndp2(n, 0); if (A[i] != -1) { int a = A[i]; for (int j = a + 1; j < n; j++) { ndp1[a] += dp1[j] + dp2[j]; } for (int j = 0; j < a; j++) { ndp2[j] += dp1[j]; } rem[a] = false; cc--; } else { int c2 = cc; for (int j = 0; j < n; j++) { if (rem[j]) { c2--; continue; } int x = n - 1 - j - (i - 1) - c2; if (x < 0) continue; ndp2[j] += dp1[j] * x; } mint tot = 0; for (int j = n - 1; j >= 0; j--) { if (rem[j]) continue; ndp1[j] += tot; tot += dp1[j] + dp2[j]; } } std::swap(dp1, ndp1); std::swap(dp2, ndp2); } mint ans = dp1[0] + dp2[0]; std::cout << ans.val() << std::endl; } int main(int argc, char *argv[]) { std::cin.tie(0)->sync_with_stdio(0); registerValidation(argc, argv); int T = inf.readInt(1, 5000); inf.readEoln(); while (T--) solve(); inf.readEof(); }