結果
| 問題 |
No.2898 Update Max
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-09-20 21:35:28 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 143 ms / 2,000 ms |
| コード長 | 1,569 bytes |
| コンパイル時間 | 142 ms |
| コンパイル使用メモリ | 82,176 KB |
| 実行使用メモリ | 115,584 KB |
| 最終ジャッジ日時 | 2024-09-20 21:35:42 |
| 合計ジャッジ時間 | 4,912 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 |
ソースコード
import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import gcd,log
input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())
def cmb(n, r, mod):
if ( r<0 or r>n ):
return 0
return (g1[n] * g2[r] % mod) * g2[n-r] % mod
mod = 998244353
N = 3*10**5
g1 = [1]*(N+1)
g2 = [1]*(N+1)
inverse = [1]*(N+1)
for i in range( 2, N + 1 ):
g1[i]=( ( g1[i-1] * i ) % mod )
inverse[i]=( ( -inverse[mod % i] * (mod//i) ) % mod )
g2[i]=( (g2[i-1] * inverse[i]) % mod )
inverse[0]=0
def solve(N,A):
A_use = [0] * (N+1)
for a in A:
if a!=-1:
A_use[a] = 1
rest_cnt_lower = [0] * (N+1)
for a in range(1,N+1):
if not A_use[a]:
rest_cnt_lower[a] += 1
for a in range(1,N+1):
rest_cnt_lower[a] += rest_cnt_lower[a-1]
res = 0
tmp_a_max = 0
vac_cnt = 0
n = rest_cnt_lower[-1]
for i in range(N):
if A[i] == -1:
vac_cnt += 1
res += g1[n] * inverse[vac_cnt] % mod - cmb(rest_cnt_lower[tmp_a_max],vac_cnt,mod) * (g1[vac_cnt-1] * g1[n-vac_cnt] % mod) % mod
res %= mod
else:
tmp_a_max = max(tmp_a_max,A[i])
if tmp_a_max == A[i]:
res += cmb(rest_cnt_lower[tmp_a_max],vac_cnt,mod) * (g1[vac_cnt] * g1[n-vac_cnt] % mod) % mod
res %= mod
return res
N = int(input())
A = li()
print(solve(N,A))