結果
| 問題 |
No.1950 片道きゃっちぼーる
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-07-24 16:46:16 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 2,150 ms / 3,000 ms |
| コード長 | 3,124 bytes |
| コンパイル時間 | 3,116 ms |
| コンパイル使用メモリ | 96,480 KB |
| 実行使用メモリ | 182,548 KB |
| 最終ジャッジ日時 | 2024-10-01 11:42:08 |
| 合計ジャッジ時間 | 37,399 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 23 |
ソースコード
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();
int[] points = new int[n];
for (int i = 0; i < n; i++) {
points[i] = sc.nextInt();
}
HashMap<Integer, ArrayList<Integer>> relays = new HashMap<>();
PriorityQueue<Path> queue = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
if (!relays.containsKey(points[i] - x)) {
relays.put(points[i] - x, new ArrayList<>());
}
relays.get(points[i] - x).add(i);
if (!relays.containsKey(points[i] + x)) {
relays.put(points[i] + x, new ArrayList<>());
}
relays.get(points[i] + x).add(i);
queue.add(new Path(i, points[i] + x));
}
int[] ans = new int[n];
while (queue.size() > 0) {
Path p = queue.poll();
if (ans[p.idx] > 0) {
continue;
}
ans[p.idx] = p.value - points[p.idx];
if (relays.containsKey(points[p.idx])) {
for (int x : relays.get(points[p.idx])) {
if (ans[x] == 0) {
queue.add(new Path(x, p.value));
}
}
}
}
System.out.println(String.join("\n", Arrays.stream(ans).mapToObj(String::valueOf).toArray(String[]::new)));
}
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 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();
}
}
tenten