結果

問題 No.2036 Max Middle
ユーザー taiga0629kyoprotaiga0629kyopro
提出日時 2022-08-12 22:16:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 195 ms / 2,000 ms
コード長 1,213 bytes
コンパイル時間 244 ms
コンパイル使用メモリ 81,736 KB
実行使用メモリ 126,224 KB
最終ジャッジ日時 2023-10-24 10:25:42
合計ジャッジ時間 3,800 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,488 KB
testcase_01 AC 38 ms
53,488 KB
testcase_02 AC 38 ms
53,488 KB
testcase_03 AC 37 ms
53,488 KB
testcase_04 AC 42 ms
53,488 KB
testcase_05 AC 39 ms
53,488 KB
testcase_06 AC 38 ms
53,488 KB
testcase_07 AC 38 ms
53,488 KB
testcase_08 AC 145 ms
99,284 KB
testcase_09 AC 186 ms
120,364 KB
testcase_10 AC 192 ms
121,876 KB
testcase_11 AC 167 ms
114,468 KB
testcase_12 AC 167 ms
114,792 KB
testcase_13 AC 195 ms
123,060 KB
testcase_14 AC 67 ms
71,340 KB
testcase_15 AC 66 ms
71,340 KB
testcase_16 AC 191 ms
126,224 KB
testcase_17 AC 187 ms
115,428 KB
testcase_18 AC 173 ms
114,768 KB
testcase_19 AC 170 ms
114,816 KB
testcase_20 AC 189 ms
114,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #



########################################
from heapq import heappush, heappop
def dijkstra( G, s, INF=10 ** 18):
    """
    https://tjkendev.github.io/procon-library/python/graph/dijkstra.html
    O((|E|+|V|)log|V|)
    V: 頂点数
    G[v] = [(nod, cost)]:
        頂点vから遷移可能な頂点(nod)とそのコスト(cost)
    s: 始点の頂点"""

    N=len(G)
    N+=1
    dist = [INF] * N
    hp = [(0, s)]  # (c, v)
    dist[s] = 0
    while hp:
        c, v = heappop(hp)  #vまで行くコストがc
        if dist[v] < c:
            continue
        for u, cost in G[v]:
            if dist[v] + cost < dist[u]:
                dist[u] = dist[v] + cost
                heappush(hp, (dist[u], u))
    return dist
##################################################


n=int(input())
a=[10**12]+list(map(int,input().split()))+[10**12]
root=[[] for i in range(n+3)]
s=n+2
root[s].append((1,0))
root[s].append((n,0))
for i in range(1,n+1):
    if a[i-1]<a[i]:
        root[i-1].append((i,1))
    else:
        root[i-1].append((i,0))
    if a[i+1]<a[i]:
        root[i+1].append((i,1))
    else:
        root[i+1].append((i,0))


d=dijkstra(root,s)
ans=0
for i in range(1,n+1):ans+=d[i]
print(ans)
0