結果
問題 | No.1767 BLUE to RED |
ユーザー | tenten |
提出日時 | 2023-04-18 11:43:46 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,198 ms / 2,000 ms |
コード長 | 3,502 bytes |
コンパイル時間 | 3,145 ms |
コンパイル使用メモリ | 84,688 KB |
実行使用メモリ | 98,404 KB |
最終ジャッジ日時 | 2024-10-13 09:54:21 |
合計ジャッジ時間 | 22,077 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 49 ms
50,380 KB |
testcase_01 | AC | 57 ms
50,396 KB |
testcase_02 | AC | 51 ms
50,332 KB |
testcase_03 | AC | 50 ms
50,316 KB |
testcase_04 | AC | 74 ms
51,244 KB |
testcase_05 | AC | 88 ms
51,492 KB |
testcase_06 | AC | 55 ms
50,056 KB |
testcase_07 | AC | 91 ms
51,284 KB |
testcase_08 | AC | 92 ms
51,228 KB |
testcase_09 | AC | 706 ms
78,204 KB |
testcase_10 | AC | 879 ms
86,164 KB |
testcase_11 | AC | 836 ms
79,432 KB |
testcase_12 | AC | 824 ms
78,344 KB |
testcase_13 | AC | 945 ms
88,864 KB |
testcase_14 | AC | 1,132 ms
93,148 KB |
testcase_15 | AC | 1,198 ms
94,984 KB |
testcase_16 | AC | 1,163 ms
95,380 KB |
testcase_17 | AC | 1,170 ms
93,156 KB |
testcase_18 | AC | 1,089 ms
94,940 KB |
testcase_19 | AC | 1,096 ms
93,424 KB |
testcase_20 | AC | 1,166 ms
98,120 KB |
testcase_21 | AC | 1,129 ms
98,240 KB |
testcase_22 | AC | 1,140 ms
98,296 KB |
testcase_23 | AC | 1,147 ms
98,404 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()); } reds.add(Integer.MIN_VALUE / 2); reds.add(Integer.MAX_VALUE); PriorityQueue<Bridge> queue = new PriorityQueue<>(); int prev = 0; for (int i = 1; i <= m; i++) { int x = sc.nextInt(); queue.add(new Bridge(0, i, x - reds.floor(x))); queue.add(new Bridge(0, i, reds.ceiling(x) - x)); if (i > 1) { queue.add(new Bridge(i - 1, i, x - prev)); } prev = x; } UnionFindTree uft = new UnionFindTree(m + 1); long ans = 0; while (queue.size() > 0) { ans += uft.unite(queue.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 parents[x]; } else { return parents[x] = find(parents[x]); } } public boolean same(Bridge b) { return find(b.left) == find(b.right); } public int unite(Bridge b) { if (same(b)) { return 0; } else { parents[find(b.right)] = find(b.left); 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(); } }