結果
問題 | No.1767 BLUE to RED |
ユーザー | tenten |
提出日時 | 2023-11-30 18:47:02 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,434 ms / 2,000 ms |
コード長 | 3,468 bytes |
コンパイル時間 | 2,977 ms |
コンパイル使用メモリ | 85,220 KB |
実行使用メモリ | 88,092 KB |
最終ジャッジ日時 | 2024-09-26 14:26:50 |
合計ジャッジ時間 | 26,496 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 60 ms
36,628 KB |
testcase_01 | AC | 60 ms
37,040 KB |
testcase_02 | AC | 60 ms
36,960 KB |
testcase_03 | AC | 60 ms
37,072 KB |
testcase_04 | AC | 86 ms
37,584 KB |
testcase_05 | AC | 93 ms
38,216 KB |
testcase_06 | AC | 66 ms
36,912 KB |
testcase_07 | AC | 106 ms
38,448 KB |
testcase_08 | AC | 107 ms
38,248 KB |
testcase_09 | AC | 907 ms
68,580 KB |
testcase_10 | AC | 1,199 ms
81,644 KB |
testcase_11 | AC | 1,000 ms
69,436 KB |
testcase_12 | AC | 885 ms
66,736 KB |
testcase_13 | AC | 1,124 ms
79,384 KB |
testcase_14 | AC | 1,399 ms
82,932 KB |
testcase_15 | AC | 1,411 ms
82,988 KB |
testcase_16 | AC | 1,399 ms
82,972 KB |
testcase_17 | AC | 1,401 ms
88,092 KB |
testcase_18 | AC | 1,434 ms
82,916 KB |
testcase_19 | AC | 1,362 ms
83,420 KB |
testcase_20 | AC | 1,365 ms
83,560 KB |
testcase_21 | AC | 1,379 ms
83,312 KB |
testcase_22 | AC | 1,385 ms
83,040 KB |
testcase_23 | AC | 1,377 ms
84,624 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(); TreeSet<Integer> reds = new TreeSet<>(); for (int i = 0; i < n; i++) { reds.add(sc.nextInt()); } int prev = -1; PriorityQueue<Bridge> bridges = new PriorityQueue<>(); for (int i = 1; i <= m; i++) { int x = sc.nextInt(); if (reds.first() <= x) { bridges.add(new Bridge(i, 0, x - reds.floor(x))); } if (reds.last() >= x) { bridges.add(new Bridge(i, 0, reds.ceiling(x) - x)); } if (i > 1) { bridges.add(new Bridge(i, i - 1, x - prev)); } prev = x; } UnionFindTree uft = new UnionFindTree(m + 1); long ans = 0; while (bridges.size() > 0) { ans += uft.unite(bridges.poll()); } 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 int unite(Bridge b) { int x = find(b.left); int y = find(b.right); if (x == y) { return 0; } else { parents[x] = y; return b.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(); } }