結果
問題 | No.1577 織姫と彦星2 |
ユーザー | tenten |
提出日時 | 2022-08-18 13:39:01 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 475 ms / 2,000 ms |
コード長 | 2,503 bytes |
コンパイル時間 | 2,500 ms |
コンパイル使用メモリ | 79,984 KB |
実行使用メモリ | 58,988 KB |
最終ジャッジ日時 | 2024-10-06 03:55:45 |
合計ジャッジ時間 | 18,516 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
37,024 KB |
testcase_01 | AC | 50 ms
36,800 KB |
testcase_02 | AC | 51 ms
36,752 KB |
testcase_03 | AC | 50 ms
37,140 KB |
testcase_04 | AC | 50 ms
36,508 KB |
testcase_05 | AC | 171 ms
45,500 KB |
testcase_06 | AC | 125 ms
42,256 KB |
testcase_07 | AC | 436 ms
52,724 KB |
testcase_08 | AC | 234 ms
48,288 KB |
testcase_09 | AC | 460 ms
58,988 KB |
testcase_10 | AC | 219 ms
47,836 KB |
testcase_11 | AC | 109 ms
40,548 KB |
testcase_12 | AC | 279 ms
49,788 KB |
testcase_13 | AC | 187 ms
45,932 KB |
testcase_14 | AC | 300 ms
50,752 KB |
testcase_15 | AC | 192 ms
47,228 KB |
testcase_16 | AC | 360 ms
52,116 KB |
testcase_17 | AC | 279 ms
48,944 KB |
testcase_18 | AC | 56 ms
37,388 KB |
testcase_19 | AC | 348 ms
51,708 KB |
testcase_20 | AC | 291 ms
50,812 KB |
testcase_21 | AC | 401 ms
52,408 KB |
testcase_22 | AC | 398 ms
51,580 KB |
testcase_23 | AC | 238 ms
47,860 KB |
testcase_24 | AC | 404 ms
52,568 KB |
testcase_25 | AC | 193 ms
46,084 KB |
testcase_26 | AC | 245 ms
47,956 KB |
testcase_27 | AC | 326 ms
50,116 KB |
testcase_28 | AC | 297 ms
49,980 KB |
testcase_29 | AC | 261 ms
48,496 KB |
testcase_30 | AC | 261 ms
48,680 KB |
testcase_31 | AC | 382 ms
52,144 KB |
testcase_32 | AC | 94 ms
39,404 KB |
testcase_33 | AC | 83 ms
38,540 KB |
testcase_34 | AC | 265 ms
48,512 KB |
testcase_35 | AC | 356 ms
51,372 KB |
testcase_36 | AC | 363 ms
51,420 KB |
testcase_37 | AC | 475 ms
55,480 KB |
testcase_38 | AC | 453 ms
54,084 KB |
testcase_39 | AC | 269 ms
48,324 KB |
testcase_40 | AC | 261 ms
48,252 KB |
testcase_41 | AC | 388 ms
51,760 KB |
testcase_42 | AC | 275 ms
49,012 KB |
testcase_43 | AC | 309 ms
49,480 KB |
testcase_44 | AC | 96 ms
39,744 KB |
testcase_45 | AC | 173 ms
44,428 KB |
testcase_46 | AC | 169 ms
45,604 KB |
testcase_47 | AC | 326 ms
51,444 KB |
testcase_48 | AC | 239 ms
47,708 KB |
testcase_49 | AC | 290 ms
48,916 KB |
testcase_50 | AC | 239 ms
47,496 KB |
testcase_51 | AC | 133 ms
42,900 KB |
testcase_52 | AC | 434 ms
53,436 KB |
testcase_53 | AC | 400 ms
53,016 KB |
testcase_54 | AC | 274 ms
48,640 KB |
testcase_55 | AC | 357 ms
52,500 KB |
testcase_56 | AC | 195 ms
47,192 KB |
testcase_57 | AC | 152 ms
44,360 KB |
ソースコード
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); HashMap<Integer, Integer> idxes = new HashMap<>(); int[] values = new int[n + 2]; ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); for (int i = 0; i < n + 2; i++) { values[i] = sc.nextInt(); idxes.put(values[i], i); graph.add(new ArrayList<>()); } for (int i = 0; i <= n + 1; i++) { int x = values[i]; for (int j = 0; j < 30; j++) { x ^= (1 << j); if (idxes.containsKey(x)) { graph.get(idxes.get(x)).add(i); } x ^= (1 << j); } } PriorityQueue<Path> queue = new PriorityQueue<>(); int[] costs = new int[n + 2]; Arrays.fill(costs, Integer.MAX_VALUE); queue.add(new Path(0, 0)); while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.idx] <= p.value) { continue; } costs[p.idx] = p.value; for (int x : graph.get(p.idx)) { queue.add(new Path(x, p.value + 1)); } } if (costs[1] == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(costs[1] - 1); } } static class Path implements Comparable<Path> { int idx; int value; public Path(int idx, int value) { this.idx = idx; this.value = value; } public int compareTo(Path another) { return value - another.value; } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }