結果
問題 | No.1767 BLUE to RED |
ユーザー | tenten |
提出日時 | 2023-01-17 08:41:12 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,253 ms / 2,000 ms |
コード長 | 3,645 bytes |
コンパイル時間 | 3,058 ms |
コンパイル使用メモリ | 86,064 KB |
実行使用メモリ | 98,820 KB |
最終ジャッジ日時 | 2024-06-10 12:50:12 |
合計ジャッジ時間 | 22,246 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 50 ms
50,632 KB |
testcase_01 | AC | 52 ms
50,300 KB |
testcase_02 | AC | 50 ms
50,580 KB |
testcase_03 | AC | 49 ms
50,236 KB |
testcase_04 | AC | 78 ms
51,136 KB |
testcase_05 | AC | 75 ms
51,124 KB |
testcase_06 | AC | 54 ms
50,224 KB |
testcase_07 | AC | 88 ms
51,676 KB |
testcase_08 | AC | 89 ms
51,512 KB |
testcase_09 | AC | 857 ms
79,020 KB |
testcase_10 | AC | 885 ms
82,436 KB |
testcase_11 | AC | 923 ms
84,048 KB |
testcase_12 | AC | 893 ms
78,804 KB |
testcase_13 | AC | 1,067 ms
91,824 KB |
testcase_14 | AC | 1,178 ms
98,748 KB |
testcase_15 | AC | 1,213 ms
98,424 KB |
testcase_16 | AC | 1,163 ms
98,820 KB |
testcase_17 | AC | 1,091 ms
95,604 KB |
testcase_18 | AC | 1,242 ms
95,040 KB |
testcase_19 | AC | 1,125 ms
95,140 KB |
testcase_20 | AC | 1,253 ms
95,584 KB |
testcase_21 | AC | 1,211 ms
98,744 KB |
testcase_22 | AC | 1,171 ms
98,488 KB |
testcase_23 | AC | 1,151 ms
95,172 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); ArrayList<Bridge> bridges = new ArrayList<>(); int prev = 0; for (int i = 1; i <= m; i++) { int x = sc.nextInt(); bridges.add(new Bridge(0, i, reds.ceiling(x) - x)); bridges.add(new Bridge(0, i, x - reds.floor(x))); if (i > 1) { bridges.add(new Bridge(i - 1, i, x - prev)); } prev = x; } Collections.sort(bridges); UnionFindTree uft = new UnionFindTree(m + 1); long ans = 0; for (Bridge x : bridges) { if (!uft.same(x)) { uft.unite(x); ans += x.value; } } System.out.println(ans); } 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(y)] = find(x); } public void unite(Bridge b) { unite(b.left, b.right); } } 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; } } } 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(); } }