結果
| 問題 |
No.1677 mæx
|
| コンテスト | |
| ユーザー |
convexineq
|
| 提出日時 | 2021-09-10 22:48:49 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 201 ms / 2,000 ms |
| コード長 | 2,613 bytes |
| コンパイル時間 | 347 ms |
| コンパイル使用メモリ | 82,376 KB |
| 実行使用メモリ | 117,560 KB |
| 最終ジャッジ日時 | 2024-06-12 02:24:32 |
| 合計ジャッジ時間 | 4,292 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 18 |
ソースコード
def Cartesian_tree(A):
n = len(A)
par = [-1]*n
order = []
for i,ai in enumerate(A):
cur = i-1
pre = -1
while cur != -1 and A[cur] > ai:
order.append(cur)
cur, pre = par[cur], cur
par[i] = cur
if pre != -1:
par[pre] = i
cur = n-1
while cur != -1:
order.append(cur)
cur = par[cur]
return par, order
def find_root_and_children(par):
n = len(par)
left_child = [-1]*n
right_child = [-1]*n
for v,p in enumerate(par):
if p != -1:
if v < p:
left_child[p] = v
else:
right_child[p] = v
else:
root = v
return root, left_child, right_child
def getLR(a,par,order):
n = len(a)
L = list(range(n))
R = list(range(n))
for v in order[:-1]:
p = par[v]
L[p] = min(L[p],L[v])
R[p] = max(R[p],R[v])
return L,R
s = input()
n = len(s)
k = int(input())
if len(s) == 1:
if s == "?":
ans = 1
else:
ans = int(k==int(s))
print(ans)
exit()
INF = 1<<30
depth = [1<<30]*n
cnt = 0
for i in range(n):
si = s[i]
if si == "(":
cnt += 1
elif si == ")":
cnt -= 1
elif si == ",":
depth[i] = cnt
par,order = Cartesian_tree(depth)
root, left_child, right_child = find_root_and_children(par)
L,R = getLR(depth,par,order)
dp = [[0]*3 for _ in range(n)]
op = [""]*n
q = [(0,n,root)]
while q:
L,R,idx = q.pop()
#print(L,R,idx)
if L+1 == R:
if s[L] == "?":
dp[idx] = [1]*3
else:
dp[idx] = [0]*3
dp[idx][int(s[L])] = 1
continue
elif depth[idx] == INF:
continue
op[idx] = s[L+1]
lc = left_child[idx]
rc = right_child[idx]
L += 4
R -= 1
if lc != -1:
q.append((L,idx,lc))
if rc != -1:
q.append((idx+1,R,rc))
def mex(i,j):
if 0 not in [i,j]: return 0
if 1 not in [i,j]: return 1
return 2
MEX = [[mex(i,j) for i in range(3)] for j in range(3)]
#print(dp)
MOD = 998244353
for v in order:
if depth[v] == INF: continue
#print(v,"v")
a = dp[left_child[v]]
b = dp[right_child[v]]
res = dp[v]
if op[v] != "e":
for i in range(3):
for j in range(3):
res[max(i,j)] += a[i]*b[j]
res[max(i,j)] %= MOD
if op[v] != "a":
for i in range(3):
for j in range(3):
res[MEX[i][j]] += a[i]*b[j]
res[MEX[i][j]] %= MOD
#print(dp[root])
print(dp[root][k]%MOD)
convexineq