結果

問題 No.2335 Jump
ユーザー ShirotsumeShirotsume
提出日時 2023-06-02 21:27:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 1,222 bytes
コンパイル時間 267 ms
コンパイル使用メモリ 86,992 KB
実行使用メモリ 99,908 KB
最終ジャッジ日時 2023-08-28 02:44:02
合計ジャッジ時間 4,642 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
74,588 KB
testcase_01 AC 111 ms
74,816 KB
testcase_02 AC 113 ms
74,696 KB
testcase_03 AC 111 ms
74,196 KB
testcase_04 AC 109 ms
74,344 KB
testcase_05 AC 110 ms
74,624 KB
testcase_06 AC 111 ms
74,544 KB
testcase_07 AC 123 ms
79,020 KB
testcase_08 AC 125 ms
79,116 KB
testcase_09 AC 122 ms
79,092 KB
testcase_10 AC 128 ms
79,020 KB
testcase_11 AC 122 ms
79,320 KB
testcase_12 AC 181 ms
99,352 KB
testcase_13 AC 177 ms
99,828 KB
testcase_14 AC 177 ms
99,552 KB
testcase_15 AC 180 ms
99,828 KB
testcase_16 AC 178 ms
99,428 KB
testcase_17 AC 180 ms
99,828 KB
testcase_18 AC 177 ms
99,448 KB
testcase_19 AC 177 ms
99,576 KB
testcase_20 AC 175 ms
99,736 KB
testcase_21 AC 180 ms
99,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, time, random
from collections import deque, Counter, defaultdict
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 63 - 1
mod = 998244353

def Dijkstra(s, graph):
    INF = 2 ** 63 - 1
    import heapq
    n = len(graph)
    dist = [INF] * n
    dist[s] = 0
    bef = [0] * n
    bef[s] = s
    hq = [(0, s)]
    heapq.heapify(hq)
    while hq:
        c, now = heapq.heappop(hq)
        
        if c > dist[now]:
            continue
        for to, cost in graph[now]:
            if dist[now] + cost < dist[to]:
                dist[to] = cost + dist[now]
                bef[to] = now
                heapq.heappush(hq, (dist[to], + to))
    return dist, bef

def DijkstraRest(bef, t):
    now = t
    ret = []
    while bef[now] != now:
        ret.append((bef[now], now))
        now = bef[now]
    ret.reverse()
    return ret

n = ii()

a = li()
graph = [[] for _ in range(n)]
for i in range(n):
    if 0 <= i - 1:
        graph[i].append((i - 1, abs(a[i] - a[i - 1]) - 1))
    if i + 1 < n:
        graph[i].append((i + 1, abs(a[i] - a[i + 1]) - 1))

d, _ = Dijkstra(0, graph)

print(d[n - 1] + a[0] - 1)
0