Submission #466963

#TimeUsernameProblemLanguageResultExecution timeMemory
466963rainliofficialSirni (COCI17_sirni)Java
56 / 140
5096 ms786432 KiB
import java.io.*;
import java.util.*;

public class sirni {
    static int n, max = 0, ind = 0;
    static TreeSet<Integer> arr;
    static ArrayList<Edge> edges = new ArrayList<>();
    public static void main(String[] args) throws IOException{
        BufferedReader file = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader file = new BufferedReader(new FileReader("file.in"));
        //PrintWriter outfile = new PrintWriter (new BufferedWriter(new FileWriter("Sirni.out")));
        n = Integer.parseInt(file.readLine());
        arr = new TreeSet<>();
        HashMap<Integer, Integer> used = new HashMap<>();
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i=0; i<n; i++){
            int curr = Integer.parseInt(file.readLine());
            if (!used.containsKey(curr)){
                used.put(curr, 0);
            }
            used.put(curr, used.get(curr)+1);
            if (!map.containsKey(curr)){
                map.put(curr, ind++);
            }
            arr.add(curr);
            max = Math.max(max, curr);
        }
        for (int i : arr){
            findMultiples(i);
        }
        Collections.sort(edges);
        DSU dsu = new DSU(ind+1);
        int ans = 0;
        for (Edge curr : edges){
            if (dsu.find(map.get(curr.from)) != dsu.find(map.get(curr.to))){
                dsu.union(map.get(curr.from), map.get(curr.to));
                ans += curr.cost;
            }
        }
        System.out.println(ans);
    }
    public static void findMultiples(int base){
        int k = 1;
        while (base*k <= max){
            if (k == 1){
                if (arr.higher(base*k) != null){
                    int val = arr.higher(base*k);
                    edges.add(new Edge(base, val, val % base)); 
                }
            }else{
                if (arr.ceiling(base*k) != null){
                    int val = arr.ceiling(base*k);
                    edges.add(new Edge(base, val, val % base)); 
                }
            }
            k++;
        }
    }
    static class Edge implements Comparable<Edge>{
        int from, to, cost;
        public Edge(int from ,int to, int cost){
            this.from = from;
            this.to = to;
            this.cost = cost;
        }
        public int compareTo(Edge o){
            return this.cost - o.cost;
        }
    }
    static class Node implements Comparable<Node>{
        int val;
        int id;
        public Node(int val ,int id){
            this.val = val;
            this.id = id;
        }
        public int compareTo(Node o){
            if (this.val == o.val){
                return this.id - o.id;
            }
            return this.val - o.val;
        }
    }
    static class DSU{
        int n;
        int[] parent;
        int[] size;
        public DSU(int n){
            this.n = n;
            parent = new int[n];
            size = new int[n];
            for (int i=0; i<n; i++){
                parent[i] = i;
                size[i] = 1;
            }
        }

        public int find(int a){
            if (parent[a] == a){
                return a;
            }
            return parent[a] = find(parent[a]); // Path compression
        }

        public void union(int a, int b){
            a = find(a);
            b = find(b);
            if (a == b){
                // Same component
                return;
            }
            if (size[a] < size[b]){
                // Point a to b
                parent[a] = b;
                size[b] += size[a];
            }else{
                // Point b to a
                parent[b] = a;
                size[a] += size[b];
            }
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...