結果
| 問題 | No.2898 Update Max |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-09-19 21:42:24 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 473 ms / 2,000 ms |
| コード長 | 1,604 bytes |
| コンパイル時間 | 144 ms |
| コンパイル使用メモリ | 82,176 KB |
| 実行使用メモリ | 221,500 KB |
| 最終ジャッジ日時 | 2024-09-19 21:50:18 |
| 合計ジャッジ時間 | 9,891 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 |
ソースコード
import bisect
n = int(input())
a = list(map(int, input().split()))
a_set = set(a)
assert 1 <= n <= 2 * 10**5
assert len(a) == n
assert all(x == -1 or 1 <= x <= n for x in a)
assert len(a_set - {-1}) == sum(x != -1 for x in a)
mod = 998244353
max_frac = 10 ** 6
frac = [1]
for i in range(1, max_frac + 1):
frac.append(frac[-1] * i % mod)
def comb(a, b):
if a < b or a < 0 or b < 0:
return 0
return frac[a] * pow(frac[b] * frac[a - b], -1, mod) % mod
a_set = set(a)
not_a = sorted([i for i in range(1, n + 1) if i not in a_set])
not_a_len = len(not_a)
prefix_max = 0
prefix_neg = 0
ans = 0
for i, x in enumerate(a):
if x == -1:
not_a_pos = max(bisect.bisect_left(not_a, prefix_max), prefix_neg)
ans += (comb(not_a_len, prefix_neg + 1) - comb(not_a_pos, prefix_neg + 1)) * frac[not_a_len - prefix_neg - 1] * frac[prefix_neg]
# print(i, x, (comb(not_a_len + 2, not_a_pos - prefix_neg + 2) - comb(not_a_pos + 1, not_a_pos - prefix_neg + 2)) * frac[not_a_len - prefix_neg] * frac[prefix_neg])
# print(f"{i=} {x=} {not_a_pos=} {prefix_neg=} {ans=}")
# print(comb(not_a_len, prefix_neg + 1) - comb(not_a_pos, prefix_neg), frac[not_a_len - prefix_neg - 1], frac[prefix_neg])
else:
if prefix_max < x:
not_a_pos = bisect.bisect_left(not_a, x)
ans += comb(not_a_pos, prefix_neg) * frac[not_a_len - prefix_neg] * frac[prefix_neg]
# print(i, x, comb(not_a_pos, prefix_neg) * frac[not_a_len - prefix_neg] * frac[prefix_neg])
prefix_max = max(prefix_max, x)
prefix_neg += x == -1
print(ans % mod)