結果

問題 No.1917 LCMST
ユーザー tentententen
提出日時 2023-04-13 13:30:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,824 ms / 4,000 ms
コード長 4,606 bytes
コンパイル時間 3,245 ms
コンパイル使用メモリ 86,524 KB
実行使用メモリ 161,008 KB
最終ジャッジ日時 2024-04-17 17:27:01
合計ジャッジ時間 52,091 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
36,924 KB
testcase_01 AC 44 ms
37,284 KB
testcase_02 AC 47 ms
37,420 KB
testcase_03 AC 116 ms
42,340 KB
testcase_04 AC 112 ms
42,032 KB
testcase_05 AC 115 ms
42,420 KB
testcase_06 AC 115 ms
42,220 KB
testcase_07 AC 105 ms
42,200 KB
testcase_08 AC 46 ms
37,316 KB
testcase_09 AC 1,824 ms
160,880 KB
testcase_10 AC 1,801 ms
160,696 KB
testcase_11 AC 1,683 ms
161,008 KB
testcase_12 AC 1,765 ms
133,784 KB
testcase_13 AC 1,228 ms
97,004 KB
testcase_14 AC 1,781 ms
156,288 KB
testcase_15 AC 981 ms
77,416 KB
testcase_16 AC 1,332 ms
96,900 KB
testcase_17 AC 1,483 ms
108,296 KB
testcase_18 AC 1,493 ms
125,568 KB
testcase_19 AC 1,022 ms
79,812 KB
testcase_20 AC 880 ms
79,412 KB
testcase_21 AC 934 ms
81,128 KB
testcase_22 AC 837 ms
78,264 KB
testcase_23 AC 928 ms
75,400 KB
testcase_24 AC 1,016 ms
78,064 KB
testcase_25 AC 879 ms
78,080 KB
testcase_26 AC 857 ms
77,304 KB
testcase_27 AC 949 ms
80,592 KB
testcase_28 AC 1,016 ms
80,456 KB
testcase_29 AC 1,617 ms
157,112 KB
testcase_30 AC 1,678 ms
157,132 KB
testcase_31 AC 1,525 ms
157,060 KB
testcase_32 AC 1,620 ms
156,912 KB
testcase_33 AC 1,694 ms
157,076 KB
testcase_34 AC 332 ms
70,360 KB
testcase_35 AC 330 ms
69,816 KB
testcase_36 AC 331 ms
70,188 KB
testcase_37 AC 1,642 ms
156,928 KB
testcase_38 AC 1,695 ms
157,208 KB
testcase_39 AC 1,566 ms
156,776 KB
testcase_40 AC 1,750 ms
157,048 KB
testcase_41 AC 1,595 ms
157,152 KB
testcase_42 AC 1,615 ms
157,600 KB
testcase_43 AC 499 ms
73,360 KB
testcase_44 AC 460 ms
72,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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[] values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        Arrays.sort(values);
        long ans = 0;
        for (int i = 0; i < n - 1; i++) {
            if (values[i] == values[i + 1]) {
                ans += values[i];
                values[i] = 0;
            }
        }
        HashMap<Integer, ArrayList<Integer>> components = new HashMap<>();
        for (int  i = 0; i < n; i++) {
            for (int j = 1; j <= Math.sqrt(values[i]); j++) {
                if (values[i] % j == 0) {
                    if (!components.containsKey(j)) {
                        components.put(j, new ArrayList<>());
                    }
                    components.get(j).add(i);
                    if (j * j < values[i]) {
                        if (!components.containsKey(values[i] / j)) {
                            components.put(values[i] / j, new ArrayList<>());
                        }
                        components.get(values[i] / j).add(i);
                    }
                }
            }
        }
        ArrayList<Bridge> bridges = new ArrayList<>();
        for (int x : components.keySet()) {
            ArrayList<Integer> list = components.get(x);
            long base = values[list.get(0)] / x;
            for (int i = 1; i < list.size(); i++) {
                bridges.add(new Bridge(list.get(0), list.get(i), base * values[list.get(i)]));
            }
        }
        Collections.sort(bridges);
        UnionFindTree uft = new UnionFindTree(n);
        for (Bridge x : bridges) {
            if (!uft.same(x)) {
                uft.unite(x);
                ans += x.value;
            }
        }
        System.out.println(ans);
    }
    
    static class Bridge implements Comparable<Bridge> {
        int left;
        int right;
        long value;
        
        public Bridge(int left, int right, long value) {
            this.left = left;
            this.right = right;
            this.value = value;
        }
        
        public int compareTo(Bridge another) {
            if (value == another.value) {
                return 0;
            } else if (value < another.value) {
                return -1;
            } else {
                return 1;
            }
        }
    }
    
    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 x) {
            return same(x.left, x.right);
        }
        
        public void unite(int x, int y) {
            parents[find(x)] = find(y);
        }
        
        public void unite(Bridge x) {
            unite(x.left, x.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();
    }
    
}
0