結果
問題 | No.1767 BLUE to RED |
ユーザー | tenten |
提出日時 | 2021-11-27 12:30:15 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 992 ms / 2,000 ms |
コード長 | 3,487 bytes |
コンパイル時間 | 1,978 ms |
コンパイル使用メモリ | 78,876 KB |
実行使用メモリ | 82,252 KB |
最終ジャッジ日時 | 2024-06-30 03:32:14 |
合計ジャッジ時間 | 17,725 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 45 ms
37,204 KB |
testcase_01 | AC | 46 ms
37,172 KB |
testcase_02 | AC | 44 ms
37,268 KB |
testcase_03 | AC | 45 ms
37,136 KB |
testcase_04 | AC | 51 ms
37,500 KB |
testcase_05 | AC | 57 ms
37,712 KB |
testcase_06 | AC | 46 ms
37,080 KB |
testcase_07 | AC | 77 ms
37,940 KB |
testcase_08 | AC | 72 ms
38,136 KB |
testcase_09 | AC | 697 ms
62,472 KB |
testcase_10 | AC | 794 ms
69,776 KB |
testcase_11 | AC | 804 ms
61,164 KB |
testcase_12 | AC | 792 ms
61,540 KB |
testcase_13 | AC | 809 ms
68,228 KB |
testcase_14 | AC | 931 ms
80,780 KB |
testcase_15 | AC | 973 ms
82,144 KB |
testcase_16 | AC | 887 ms
78,624 KB |
testcase_17 | AC | 932 ms
81,044 KB |
testcase_18 | AC | 931 ms
81,196 KB |
testcase_19 | AC | 946 ms
82,176 KB |
testcase_20 | AC | 944 ms
80,984 KB |
testcase_21 | AC | 992 ms
82,012 KB |
testcase_22 | AC | 963 ms
82,252 KB |
testcase_23 | AC | 939 ms
81,992 KB |
ソースコード
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(); } }