結果

問題 No.1767 BLUE to RED
ユーザー tentententen
提出日時 2023-07-20 08:38:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,508 ms / 2,000 ms
コード長 3,619 bytes
コンパイル時間 4,593 ms
コンパイル使用メモリ 95,732 KB
実行使用メモリ 118,356 KB
最終ジャッジ日時 2023-10-20 09:57:09
合計ジャッジ時間 28,736 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
53,692 KB
testcase_01 AC 57 ms
52,604 KB
testcase_02 AC 60 ms
53,672 KB
testcase_03 AC 60 ms
53,680 KB
testcase_04 AC 86 ms
54,556 KB
testcase_05 AC 104 ms
52,984 KB
testcase_06 AC 63 ms
53,696 KB
testcase_07 AC 110 ms
54,812 KB
testcase_08 AC 108 ms
55,084 KB
testcase_09 AC 982 ms
92,020 KB
testcase_10 AC 1,139 ms
93,788 KB
testcase_11 AC 992 ms
74,424 KB
testcase_12 AC 953 ms
73,132 KB
testcase_13 AC 1,203 ms
87,252 KB
testcase_14 AC 1,508 ms
107,192 KB
testcase_15 AC 1,470 ms
117,904 KB
testcase_16 AC 1,454 ms
117,708 KB
testcase_17 AC 1,503 ms
118,356 KB
testcase_18 AC 1,471 ms
117,300 KB
testcase_19 AC 1,447 ms
117,956 KB
testcase_20 AC 1,438 ms
117,912 KB
testcase_21 AC 1,446 ms
117,668 KB
testcase_22 AC 1,489 ms
118,188 KB
testcase_23 AC 1,431 ms
117,572 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 m = sc.nextInt();
        TreeMap<Integer, Integer> places = new TreeMap<>();
        for (int i = 0; i < n + m; i++) {
            places.put(sc.nextInt(), i);
        }
        UnionFindTree uft = new UnionFindTree(n + m);
        for (int i = 1; i < n; i++) {
            uft.unite(i, 0);
        }
        int prev = places.firstKey();
        int pIdx = places.get(prev);
        places.remove(prev);
        PriorityQueue<Bridge> bridges = new PriorityQueue<>();
        for (int x : places.keySet()) {
            int idx = places.get(x);
            bridges.add(new Bridge(idx, pIdx, x - prev));
            prev = x;
            pIdx = idx;
        }
        long ans = 0;
        while (bridges.size() > 0) {
            Bridge x = bridges.poll();
            if (!uft.same(x)) {
                uft.unite(x);
                ans += x.value;
            }
        }
        System.out.println(ans);
    }
    
    static class Bridge implements Comparable<Bridge> {
        int left;
        int right;
        int value;
        
        public Bridge(int left, int right, int value) {
            this.left = left;
            this.right = right;
            this.value = value;
        }
        
        public int compareTo(Bridge another) {
            return value - another.value;
        }
    }
    
    static class UnionFindTree {
        int[] parents;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public boolean same(Bridge b) {
            return same(b.left, b.right);
        }
        
        public void unite(int x, int y) {
            parents[find(x)] = find(y);
        }
        
        public void unite(Bridge b) {
            unite(b.left, b.right);
        }
    }
}
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