結果

問題 No.1950 片道きゃっちぼーる
ユーザー tentententen
提出日時 2023-07-24 16:46:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,094 ms / 3,000 ms
コード長 3,124 bytes
コンパイル時間 2,996 ms
コンパイル使用メモリ 87,792 KB
実行使用メモリ 190,044 KB
最終ジャッジ日時 2024-04-08 20:45:50
合計ジャッジ時間 36,238 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
54,640 KB
testcase_01 AC 64 ms
54,664 KB
testcase_02 AC 65 ms
54,632 KB
testcase_03 AC 2,094 ms
176,804 KB
testcase_04 AC 2,025 ms
174,228 KB
testcase_05 AC 64 ms
54,632 KB
testcase_06 AC 1,068 ms
135,028 KB
testcase_07 AC 1,319 ms
150,412 KB
testcase_08 AC 1,852 ms
161,408 KB
testcase_09 AC 1,910 ms
168,240 KB
testcase_10 AC 1,463 ms
125,388 KB
testcase_11 AC 1,507 ms
128,076 KB
testcase_12 AC 1,564 ms
126,580 KB
testcase_13 AC 1,395 ms
126,504 KB
testcase_14 AC 1,484 ms
141,340 KB
testcase_15 AC 2,087 ms
190,044 KB
testcase_16 AC 1,632 ms
132,752 KB
testcase_17 AC 67 ms
52,816 KB
testcase_18 AC 1,687 ms
157,108 KB
testcase_19 AC 1,951 ms
164,820 KB
testcase_20 AC 1,244 ms
125,080 KB
testcase_21 AC 1,445 ms
139,752 KB
testcase_22 AC 1,454 ms
139,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
    
}
0