結果

問題 No.1917 LCMST
ユーザー tentententen
提出日時 2023-04-13 13:30:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,341 ms / 4,000 ms
コード長 4,606 bytes
コンパイル時間 3,148 ms
コンパイル使用メモリ 85,616 KB
実行使用メモリ 161,008 KB
最終ジャッジ日時 2024-10-09 11:34:30
合計ジャッジ時間 64,725 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
36,864 KB
testcase_01 AC 51 ms
37,268 KB
testcase_02 AC 51 ms
37,316 KB
testcase_03 AC 141 ms
42,228 KB
testcase_04 AC 143 ms
43,008 KB
testcase_05 AC 145 ms
41,884 KB
testcase_06 AC 139 ms
42,028 KB
testcase_07 AC 137 ms
41,948 KB
testcase_08 AC 51 ms
37,292 KB
testcase_09 AC 2,284 ms
160,960 KB
testcase_10 AC 2,341 ms
160,384 KB
testcase_11 AC 2,196 ms
161,008 KB
testcase_12 AC 2,310 ms
133,452 KB
testcase_13 AC 1,564 ms
97,584 KB
testcase_14 AC 2,310 ms
156,044 KB
testcase_15 AC 1,171 ms
77,820 KB
testcase_16 AC 1,593 ms
93,824 KB
testcase_17 AC 1,891 ms
107,952 KB
testcase_18 AC 1,909 ms
125,640 KB
testcase_19 AC 1,192 ms
80,980 KB
testcase_20 AC 907 ms
76,128 KB
testcase_21 AC 1,180 ms
77,424 KB
testcase_22 AC 1,010 ms
79,540 KB
testcase_23 AC 1,033 ms
76,012 KB
testcase_24 AC 1,218 ms
80,360 KB
testcase_25 AC 1,070 ms
77,984 KB
testcase_26 AC 1,032 ms
79,240 KB
testcase_27 AC 1,157 ms
79,828 KB
testcase_28 AC 1,137 ms
80,436 KB
testcase_29 AC 2,030 ms
156,984 KB
testcase_30 AC 1,972 ms
157,136 KB
testcase_31 AC 2,016 ms
156,692 KB
testcase_32 AC 2,119 ms
157,024 KB
testcase_33 AC 2,083 ms
157,052 KB
testcase_34 AC 401 ms
70,848 KB
testcase_35 AC 400 ms
70,408 KB
testcase_36 AC 402 ms
69,732 KB
testcase_37 AC 2,077 ms
156,964 KB
testcase_38 AC 2,162 ms
157,148 KB
testcase_39 AC 2,027 ms
156,908 KB
testcase_40 AC 2,166 ms
157,076 KB
testcase_41 AC 2,114 ms
156,660 KB
testcase_42 AC 2,209 ms
156,892 KB
testcase_43 AC 587 ms
73,148 KB
testcase_44 AC 581 ms
71,756 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