結果

問題 No.1378 Flattening
ユーザー mkawa2mkawa2
提出日時 2022-06-28 02:17:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 586 ms / 2,000 ms
コード長 1,477 bytes
コンパイル時間 365 ms
コンパイル使用メモリ 82,564 KB
実行使用メモリ 79,516 KB
最終ジャッジ日時 2024-04-30 15:30:49
合計ジャッジ時間 10,876 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,748 KB
testcase_01 AC 42 ms
53,760 KB
testcase_02 AC 42 ms
53,380 KB
testcase_03 AC 41 ms
53,748 KB
testcase_04 AC 41 ms
52,592 KB
testcase_05 AC 41 ms
52,948 KB
testcase_06 AC 44 ms
53,476 KB
testcase_07 AC 41 ms
53,344 KB
testcase_08 AC 41 ms
53,564 KB
testcase_09 AC 41 ms
52,964 KB
testcase_10 AC 41 ms
53,092 KB
testcase_11 AC 40 ms
52,880 KB
testcase_12 AC 41 ms
53,336 KB
testcase_13 AC 41 ms
52,692 KB
testcase_14 AC 41 ms
53,928 KB
testcase_15 AC 41 ms
52,740 KB
testcase_16 AC 40 ms
52,612 KB
testcase_17 AC 42 ms
53,828 KB
testcase_18 AC 41 ms
52,688 KB
testcase_19 AC 41 ms
54,004 KB
testcase_20 AC 42 ms
54,244 KB
testcase_21 AC 41 ms
52,724 KB
testcase_22 AC 40 ms
53,840 KB
testcase_23 AC 42 ms
53,752 KB
testcase_24 AC 42 ms
53,968 KB
testcase_25 AC 41 ms
53,152 KB
testcase_26 AC 42 ms
53,760 KB
testcase_27 AC 43 ms
53,484 KB
testcase_28 AC 42 ms
53,072 KB
testcase_29 AC 42 ms
52,880 KB
testcase_30 AC 40 ms
53,108 KB
testcase_31 AC 485 ms
78,964 KB
testcase_32 AC 586 ms
79,280 KB
testcase_33 AC 397 ms
79,208 KB
testcase_34 AC 490 ms
79,268 KB
testcase_35 AC 450 ms
79,196 KB
testcase_36 AC 434 ms
79,188 KB
testcase_37 AC 427 ms
79,048 KB
testcase_38 AC 443 ms
79,516 KB
testcase_39 AC 429 ms
79,256 KB
testcase_40 AC 430 ms
79,268 KB
testcase_41 AC 443 ms
79,200 KB
testcase_42 AC 421 ms
79,184 KB
testcase_43 AC 431 ms
79,340 KB
testcase_44 AC 439 ms
79,244 KB
testcase_45 AC 111 ms
76,796 KB
testcase_46 AC 135 ms
76,800 KB
testcase_47 AC 77 ms
74,248 KB
testcase_48 AC 365 ms
78,408 KB
testcase_49 AC 390 ms
78,616 KB
testcase_50 AC 105 ms
76,924 KB
testcase_51 AC 48 ms
60,876 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