結果

問題 No.1577 織姫と彦星2
ユーザー ygd.ygd.
提出日時 2021-06-30 00:12:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 312 ms / 2,000 ms
コード長 1,286 bytes
コンパイル時間 760 ms
コンパイル使用メモリ 86,620 KB
実行使用メモリ 96,832 KB
最終ジャッジ日時 2023-09-08 09:40:35
合計ジャッジ時間 14,412 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,424 KB
testcase_01 AC 96 ms
71,416 KB
testcase_02 AC 95 ms
71,400 KB
testcase_03 AC 94 ms
71,260 KB
testcase_04 AC 96 ms
71,264 KB
testcase_05 AC 132 ms
78,148 KB
testcase_06 AC 109 ms
77,868 KB
testcase_07 AC 267 ms
91,384 KB
testcase_08 AC 175 ms
83,272 KB
testcase_09 AC 312 ms
96,832 KB
testcase_10 AC 154 ms
80,964 KB
testcase_11 AC 128 ms
77,764 KB
testcase_12 AC 198 ms
87,012 KB
testcase_13 AC 136 ms
78,796 KB
testcase_14 AC 215 ms
89,604 KB
testcase_15 AC 161 ms
81,520 KB
testcase_16 AC 257 ms
91,236 KB
testcase_17 AC 195 ms
85,436 KB
testcase_18 AC 107 ms
77,128 KB
testcase_19 AC 246 ms
90,100 KB
testcase_20 AC 225 ms
87,152 KB
testcase_21 AC 259 ms
90,856 KB
testcase_22 AC 250 ms
90,928 KB
testcase_23 AC 175 ms
82,472 KB
testcase_24 AC 251 ms
95,564 KB
testcase_25 AC 160 ms
80,480 KB
testcase_26 AC 176 ms
82,816 KB
testcase_27 AC 214 ms
91,696 KB
testcase_28 AC 208 ms
88,144 KB
testcase_29 AC 171 ms
85,288 KB
testcase_30 AC 187 ms
85,212 KB
testcase_31 AC 244 ms
90,784 KB
testcase_32 AC 111 ms
77,552 KB
testcase_33 AC 106 ms
77,172 KB
testcase_34 AC 171 ms
84,332 KB
testcase_35 AC 235 ms
89,416 KB
testcase_36 AC 238 ms
90,572 KB
testcase_37 AC 279 ms
96,320 KB
testcase_38 AC 278 ms
95,936 KB
testcase_39 AC 175 ms
83,540 KB
testcase_40 AC 170 ms
83,820 KB
testcase_41 AC 235 ms
94,024 KB
testcase_42 AC 180 ms
86,976 KB
testcase_43 AC 186 ms
87,256 KB
testcase_44 AC 114 ms
77,560 KB
testcase_45 AC 143 ms
78,432 KB
testcase_46 AC 134 ms
78,252 KB
testcase_47 AC 193 ms
93,096 KB
testcase_48 AC 154 ms
81,396 KB
testcase_49 AC 187 ms
85,120 KB
testcase_50 AC 161 ms
82,248 KB
testcase_51 AC 110 ms
77,700 KB
testcase_52 AC 247 ms
95,748 KB
testcase_53 AC 251 ms
95,592 KB
testcase_54 AC 177 ms
84,952 KB
testcase_55 AC 248 ms
95,768 KB
testcase_56 AC 162 ms
81,536 KB
testcase_57 AC 139 ms
78,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

def main():
    twos = [1]
    MAX = 35
    for i in range(MAX):
        twos.append(twos[-1]*2)
    twos = set(twos)
    #print(twos)

    N = int(input()); INF = float("inf")
    N += 2
    start,end = map(int,input().split())
    stones = [start] + list(map(int,input().split())) + [end]
    dic = {}
    for i in range(N):
        dic[stones[i]] = i
    G = [[] for _ in range(N)]
    used = set([])
    for stone in stones:
        base = 1
        for i in range(MAX):
            if (stone>>i)&1 == 1:
                adj = stone - base
            else:
                adj = stone + base
            base *= 2
            if adj not in dic: continue
            u = dic[stone]
            v = dic[adj]
            if v < u: #常にu < vの関係
                u,v = v,u
            if (u,v) in used: continue
            G[u].append(v)
            G[v].append(u)
            used.add((u,v))
    #print(G)
    D = [-1]*N
    Q = deque([])
    Q.append(0)
    D[0] = 0
    while Q:
        v = Q.popleft()
        for u in G[v]:
            if D[u] != -1: continue
            D[u] = D[v] + 1
            Q.append(u)
    #print(D,N)
    if D[N-1] == -1:
        print(-1)
    else:
        print(D[N-1] - 1)

if __name__ == '__main__':
    main()
0