結果

問題 No.1767 BLUE to RED
ユーザー tentententen
提出日時 2021-11-27 12:30:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,166 ms / 2,000 ms
コード長 3,487 bytes
コンパイル時間 2,392 ms
コンパイル使用メモリ 74,824 KB
実行使用メモリ 93,316 KB
最終ジャッジ日時 2023-09-12 15:36:19
合計ジャッジ時間 20,318 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,700 KB
testcase_01 AC 43 ms
49,616 KB
testcase_02 AC 44 ms
49,712 KB
testcase_03 AC 43 ms
49,760 KB
testcase_04 AC 58 ms
50,816 KB
testcase_05 AC 73 ms
50,876 KB
testcase_06 AC 47 ms
49,636 KB
testcase_07 AC 69 ms
50,832 KB
testcase_08 AC 74 ms
52,400 KB
testcase_09 AC 818 ms
72,680 KB
testcase_10 AC 901 ms
82,844 KB
testcase_11 AC 842 ms
74,148 KB
testcase_12 AC 837 ms
74,668 KB
testcase_13 AC 943 ms
78,896 KB
testcase_14 AC 1,021 ms
91,460 KB
testcase_15 AC 1,097 ms
92,228 KB
testcase_16 AC 1,024 ms
90,884 KB
testcase_17 AC 1,062 ms
90,620 KB
testcase_18 AC 1,053 ms
90,632 KB
testcase_19 AC 1,014 ms
91,620 KB
testcase_20 AC 1,047 ms
92,084 KB
testcase_21 AC 1,166 ms
93,316 KB
testcase_22 AC 1,045 ms
91,272 KB
testcase_23 AC 1,086 ms
90,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 m = sc.nextInt();
        Field[] fields = new Field[n + m];
        for (int i = 0; i < n; i++) {
            fields[i] = new Field(true, sc.nextInt());
        }
        for (int i = n; i < n + m; i++) {
            fields[i] = new Field(false, sc.nextInt());
        }
        Arrays.sort(fields);
        UnionFindTree uft = new UnionFindTree(n + m + 1);
        ArrayList<Path> list = new ArrayList<>();
        for (int i = 0; i < n + m - 1; i++) {
            if (fields[i].isRed) {
                uft.unite(i, n + m);
            }
            list.add(new Path(i, i + 1, fields[i + 1].value - fields[i].value));
        }
        if (fields[n + m - 1].isRed) {
            uft.unite(n + m - 1, n + m);
        }
        Collections.sort(list);
        long ans = 0;
        for (Path p : list) {
            if (!uft.same(p)) {
                uft.unite(p);
                ans += p.value;
            }
        }
        System.out.println(ans);
    }
    
    static class Field implements Comparable<Field> {
        boolean isRed;
        int value;
        
        public Field(boolean isRed, int value) {
            this.isRed = isRed;
            this.value = value;
        }
        
        public int compareTo(Field another) {
            return value - another.value;
        }
    }
    
    static class Path implements Comparable<Path> {
        int left;
        int right;
        int value;
        
        public Path(int left, int right, int value) {
            this.left = left;
            this.right = right;
            this.value = value;
        }
        
        public int compareTo(Path 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 (parents[x] == 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(Path p) {
            return same(p.left, p.right);
        }
        
        public void unite(int x, int y) {
            if (!same(x, y)) {
                parents[find(x)] = find(y);
            }
        }
        
        public void unite(Path p) {
            unite(p.left, p.right);
        }
    }
}

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