結果
問題 | No.1577 織姫と彦星2 |
ユーザー | tenten |
提出日時 | 2023-07-18 17:10:31 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 424 ms / 2,000 ms |
コード長 | 3,046 bytes |
コンパイル時間 | 2,442 ms |
コンパイル使用メモリ | 90,464 KB |
実行使用メモリ | 64,336 KB |
最終ジャッジ日時 | 2024-09-18 16:30:16 |
合計ジャッジ時間 | 18,077 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 48 ms
50,620 KB |
testcase_01 | AC | 49 ms
50,524 KB |
testcase_02 | AC | 47 ms
50,140 KB |
testcase_03 | AC | 51 ms
50,088 KB |
testcase_04 | AC | 50 ms
50,360 KB |
testcase_05 | AC | 175 ms
55,800 KB |
testcase_06 | AC | 112 ms
54,864 KB |
testcase_07 | AC | 345 ms
61,668 KB |
testcase_08 | AC | 231 ms
59,028 KB |
testcase_09 | AC | 424 ms
63,832 KB |
testcase_10 | AC | 208 ms
58,868 KB |
testcase_11 | AC | 119 ms
54,708 KB |
testcase_12 | AC | 279 ms
62,020 KB |
testcase_13 | AC | 235 ms
56,744 KB |
testcase_14 | AC | 289 ms
61,928 KB |
testcase_15 | AC | 186 ms
56,772 KB |
testcase_16 | AC | 339 ms
61,696 KB |
testcase_17 | AC | 301 ms
60,032 KB |
testcase_18 | AC | 65 ms
50,720 KB |
testcase_19 | AC | 400 ms
62,016 KB |
testcase_20 | AC | 325 ms
61,672 KB |
testcase_21 | AC | 336 ms
61,836 KB |
testcase_22 | AC | 341 ms
62,640 KB |
testcase_23 | AC | 215 ms
59,476 KB |
testcase_24 | AC | 365 ms
63,440 KB |
testcase_25 | AC | 174 ms
56,520 KB |
testcase_26 | AC | 238 ms
60,064 KB |
testcase_27 | AC | 300 ms
61,940 KB |
testcase_28 | AC | 290 ms
60,236 KB |
testcase_29 | AC | 233 ms
60,124 KB |
testcase_30 | AC | 248 ms
60,196 KB |
testcase_31 | AC | 335 ms
61,852 KB |
testcase_32 | AC | 102 ms
51,928 KB |
testcase_33 | AC | 79 ms
51,716 KB |
testcase_34 | AC | 228 ms
59,336 KB |
testcase_35 | AC | 309 ms
61,740 KB |
testcase_36 | AC | 318 ms
61,664 KB |
testcase_37 | AC | 375 ms
64,044 KB |
testcase_38 | AC | 363 ms
63,460 KB |
testcase_39 | AC | 246 ms
59,592 KB |
testcase_40 | AC | 239 ms
59,648 KB |
testcase_41 | AC | 331 ms
61,880 KB |
testcase_42 | AC | 269 ms
59,480 KB |
testcase_43 | AC | 256 ms
60,000 KB |
testcase_44 | AC | 89 ms
51,892 KB |
testcase_45 | AC | 159 ms
56,092 KB |
testcase_46 | AC | 166 ms
56,384 KB |
testcase_47 | AC | 293 ms
61,392 KB |
testcase_48 | AC | 228 ms
59,676 KB |
testcase_49 | AC | 250 ms
59,800 KB |
testcase_50 | AC | 213 ms
57,884 KB |
testcase_51 | AC | 128 ms
55,036 KB |
testcase_52 | AC | 330 ms
63,952 KB |
testcase_53 | AC | 329 ms
64,336 KB |
testcase_54 | AC | 227 ms
59,056 KB |
testcase_55 | AC | 335 ms
61,836 KB |
testcase_56 | AC | 183 ms
57,040 KB |
testcase_57 | AC | 146 ms
55,624 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); for (int i = 0; i < n + 2; i++) { graph.add(new ArrayList<>()); } HashMap<Integer, Integer> values = new HashMap<>(); for (int i = 0; i < n + 2; i++) { values.put(sc.nextInt(), i); } for (int x : values.keySet()) { int y = x; for (int i = 0; i < 30; i++) { y ^= (1 << i); if (values.containsKey(y)) { graph.get(values.get(x)).add(values.get(y)); } y ^= (1 << i); } } PriorityQueue<Path> queue = new PriorityQueue<>(); queue.add(new Path(0, 0)); int[] costs = new int[n + 2]; Arrays.fill(costs, Integer.MAX_VALUE); 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 Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); 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 int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }