結果
問題 | No.1950 片道きゃっちぼーる |
ユーザー |
![]() |
提出日時 | 2022-05-26 09:02:05 |
言語 | Java (openjdk 23) |
結果 |
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 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 23 |
ソースコード
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(); } }