結果
問題 | No.1767 BLUE to RED |
ユーザー | tenten |
提出日時 | 2023-07-20 08:38:07 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,393 ms / 2,000 ms |
コード長 | 3,619 bytes |
コンパイル時間 | 2,901 ms |
コンパイル使用メモリ | 96,088 KB |
実行使用メモリ | 107,144 KB |
最終ジャッジ日時 | 2024-09-20 05:26:20 |
合計ジャッジ時間 | 24,374 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 50 ms
37,268 KB |
testcase_01 | AC | 50 ms
37,056 KB |
testcase_02 | AC | 50 ms
37,008 KB |
testcase_03 | AC | 49 ms
37,228 KB |
testcase_04 | AC | 67 ms
37,824 KB |
testcase_05 | AC | 77 ms
38,428 KB |
testcase_06 | AC | 54 ms
37,100 KB |
testcase_07 | AC | 85 ms
38,608 KB |
testcase_08 | AC | 113 ms
40,812 KB |
testcase_09 | AC | 828 ms
77,404 KB |
testcase_10 | AC | 1,071 ms
82,560 KB |
testcase_11 | AC | 880 ms
74,476 KB |
testcase_12 | AC | 876 ms
75,544 KB |
testcase_13 | AC | 1,059 ms
87,584 KB |
testcase_14 | AC | 1,293 ms
104,940 KB |
testcase_15 | AC | 1,321 ms
104,892 KB |
testcase_16 | AC | 1,334 ms
107,144 KB |
testcase_17 | AC | 1,206 ms
93,688 KB |
testcase_18 | AC | 1,282 ms
104,916 KB |
testcase_19 | AC | 1,316 ms
105,384 KB |
testcase_20 | AC | 1,393 ms
105,280 KB |
testcase_21 | AC | 1,369 ms
104,700 KB |
testcase_22 | AC | 1,267 ms
105,064 KB |
testcase_23 | AC | 1,263 ms
104,976 KB |
ソースコード
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(); } }