結果

問題 No.1827 最長部分スーパーリッチ門松列列
ユーザー rin204
提出日時 2022-01-28 22:07:00
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,037 bytes
コンパイル時間 332 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 178,344 KB
最終ジャッジ日時 2024-12-30 07:02:11
合計ジャッジ時間 8,044 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 1 WA * 23
権限があれば一括ダウンロードができます

ソースコード

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