結果

問題 No.1378 Flattening
ユーザー mkawa2mkawa2
提出日時 2022-06-28 02:17:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 595 ms / 2,000 ms
コード長 1,477 bytes
コンパイル時間 370 ms
コンパイル使用メモリ 87,176 KB
実行使用メモリ 80,636 KB
最終ジャッジ日時 2023-08-12 20:25:23
合計ジャッジ時間 13,046 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,328 KB
testcase_01 AC 73 ms
71,292 KB
testcase_02 AC 74 ms
71,364 KB
testcase_03 AC 77 ms
71,356 KB
testcase_04 AC 72 ms
71,476 KB
testcase_05 AC 74 ms
71,300 KB
testcase_06 AC 76 ms
71,076 KB
testcase_07 AC 75 ms
71,452 KB
testcase_08 AC 76 ms
71,496 KB
testcase_09 AC 73 ms
71,220 KB
testcase_10 AC 74 ms
71,568 KB
testcase_11 AC 74 ms
71,196 KB
testcase_12 AC 75 ms
71,288 KB
testcase_13 AC 75 ms
71,276 KB
testcase_14 AC 75 ms
71,476 KB
testcase_15 AC 73 ms
71,312 KB
testcase_16 AC 74 ms
71,372 KB
testcase_17 AC 75 ms
71,328 KB
testcase_18 AC 75 ms
71,368 KB
testcase_19 AC 76 ms
71,348 KB
testcase_20 AC 76 ms
71,364 KB
testcase_21 AC 77 ms
71,560 KB
testcase_22 AC 75 ms
71,348 KB
testcase_23 AC 76 ms
71,616 KB
testcase_24 AC 74 ms
71,272 KB
testcase_25 AC 75 ms
71,496 KB
testcase_26 AC 76 ms
71,560 KB
testcase_27 AC 77 ms
71,184 KB
testcase_28 AC 77 ms
71,604 KB
testcase_29 AC 74 ms
71,064 KB
testcase_30 AC 74 ms
71,504 KB
testcase_31 AC 486 ms
80,056 KB
testcase_32 AC 595 ms
80,376 KB
testcase_33 AC 415 ms
80,612 KB
testcase_34 AC 501 ms
80,388 KB
testcase_35 AC 460 ms
80,084 KB
testcase_36 AC 445 ms
80,392 KB
testcase_37 AC 429 ms
80,444 KB
testcase_38 AC 449 ms
80,160 KB
testcase_39 AC 435 ms
80,636 KB
testcase_40 AC 435 ms
80,256 KB
testcase_41 AC 448 ms
80,532 KB
testcase_42 AC 426 ms
80,568 KB
testcase_43 AC 435 ms
80,112 KB
testcase_44 AC 445 ms
80,348 KB
testcase_45 AC 137 ms
77,796 KB
testcase_46 AC 158 ms
78,028 KB
testcase_47 AC 105 ms
77,012 KB
testcase_48 AC 372 ms
79,460 KB
testcase_49 AC 400 ms
79,748 KB
testcase_50 AC 132 ms
77,616 KB
testcase_51 AC 81 ms
76,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
# md = 10**9+7
md = 998244353

n = II()
aa = LI()

ll = [0]*n
rr = [0]*n

st = [(-1, -1)]
for i, a in enumerate(aa):
    while st[-1][-1] > a: st.pop()
    ll[i] = st[-1][0]+1
    st.append((i, a))
# pDB(ll)

st = [(n, -1)]
for i in range(n)[::-1]:
    a = aa[i]
    while st[-1][-1] > a: st.pop()
    rr[i] = st[-1][0]
    st.append((i, a))
# pDB(rr)

dp = [1]+[0]*n
cs = [0]+[1]*(n+1)

for i, a in enumerate(aa):
    ndp = [0]*(n+1)
    ncs = [0]*(n+2)
    l, r = ll[i], rr[i]
    for j in range(n+1):
        ndp[j] = dp[j]
        if l <= j <= r and (j > i or r < n):
            if l == 0 and j > i:
                ndp[j] += cs[i+1]-cs[l]
            else:
                ndp[j] += cs[j]-cs[l]
        ncs[j+1] = (ncs[j]+ndp[j])%md
    dp = ndp
    cs = ncs

print(dp[n]%md)
0