結果

問題 No.1827 最長部分スーパーリッチ門松列列
ユーザー rin204rin204
提出日時 2022-01-28 22:07:00
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,037 bytes
コンパイル時間 363 ms
コンパイル使用メモリ 82,572 KB
実行使用メモリ 178,888 KB
最終ジャッジ日時 2024-06-09 15:12:22
合計ジャッジ時間 6,840 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,092 KB
testcase_01 AC 36 ms
52,868 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve():
    n = int(input())
    A = list(map(int, input().split()))
    stack = []
    edges = [[] for _ in range(n)]
    
    for i, a in enumerate(A):
        while stack and a < stack[-1][0]:
            b, j = stack.pop()
            if stack and stack[-1][0] > a:
                edges[stack[-1][1]].append(j)
            else:
                edges[i].append(j)
        stack.append((a, i))
    while len(stack) >= 2:
        _, i = stack.pop()
        edges[stack[-1][1]].append(i)
        
    i = stack[0][1]
    lst = [i]
    used = [False] * n
    used[i] = True
    ans = 1
    while lst:
        pos = lst.pop()
        for npos in edges[pos]:
            if npos != 0 and used[npos - 1]:
                continue
            if npos != n - 1 and used[npos + 1]:
                continue
            used[npos] = True
            lst.append(npos)
            ans += 1
    ans = 2 * ans + 1
    if used[0]:
        ans -= 1
    if used[-1]:
        ans -= 1
    print(ans)
    
for _ in range(int(input())):
    solve()
0