結果
問題 | No.1950 片道きゃっちぼーる |
ユーザー | tenten |
提出日時 | 2022-05-26 09:02:05 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,452 ms / 3,000 ms |
コード長 | 2,703 bytes |
コンパイル時間 | 2,989 ms |
コンパイル使用メモリ | 80,040 KB |
実行使用メモリ | 114,908 KB |
最終ジャッジ日時 | 2024-09-20 14:56:17 |
合計ジャッジ時間 | 25,995 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 46 ms
36,948 KB |
testcase_01 | AC | 48 ms
36,788 KB |
testcase_02 | AC | 46 ms
36,820 KB |
testcase_03 | AC | 1,063 ms
103,384 KB |
testcase_04 | AC | 1,145 ms
103,972 KB |
testcase_05 | AC | 46 ms
36,832 KB |
testcase_06 | AC | 1,232 ms
114,908 KB |
testcase_07 | AC | 1,015 ms
86,992 KB |
testcase_08 | AC | 1,024 ms
87,092 KB |
testcase_09 | AC | 1,152 ms
104,848 KB |
testcase_10 | AC | 1,024 ms
96,516 KB |
testcase_11 | AC | 1,136 ms
96,964 KB |
testcase_12 | AC | 1,049 ms
96,208 KB |
testcase_13 | AC | 1,085 ms
100,944 KB |
testcase_14 | AC | 1,194 ms
108,492 KB |
testcase_15 | AC | 1,400 ms
97,920 KB |
testcase_16 | AC | 1,154 ms
113,368 KB |
testcase_17 | AC | 48 ms
37,116 KB |
testcase_18 | AC | 1,053 ms
93,780 KB |
testcase_19 | AC | 1,452 ms
97,964 KB |
testcase_20 | AC | 888 ms
87,304 KB |
testcase_21 | AC | 1,165 ms
105,256 KB |
testcase_22 | AC | 1,260 ms
106,548 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(); int[] positions = new int[n]; TreeMap<Integer, Integer> map = new TreeMap<>(); ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { positions[i] = sc.nextInt(); map.put(positions[i], i); graph.add(new ArrayList<>()); } int[] distances = new int[n]; for (int i = 0; i < n; i++) { distances[i] = sc.nextInt(); } for (int i = 0; i < n; i++) { if (map.containsKey(positions[i] + distances[i])) { graph.get(map.get(positions[i] + distances[i])).add(i); } if (map.containsKey(positions[i] - distances[i])) { graph.get(map.get(positions[i] - distances[i])).add(i); } } PriorityQueue<Path> queue = new PriorityQueue<>(); int[] ans = new int[n]; for (int i = 0; i < n; i++) { queue.add(new Path(i, positions[i] + distances[i])); } while (queue.size() > 0) { Path p = queue.poll(); if (ans[p.idx] >= p.value) { continue; } ans[p.idx] = p.value; for (int x : graph.get(p.idx)) { queue.add(new Path(x, p.value)); } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(ans[i] - positions[i]).append("\n"); } System.out.print(sb); } 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 another.value - 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 { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }