Submission #466957

# Submission time Handle Problem Language Result Execution time Memory
466957 2021-08-21T03:58:44 Z rainliofficial Sirni (COCI17_sirni) Java 11
0 / 140
2584 ms 130728 KB
import java.io.*;
import java.util.*;

public class sirni {
    static int n, max;
    static TreeSet<Node> 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"));
        //rintWriter outfile = new PrintWriter (new BufferedWriter(new FileWriter("Sirni.out")));
        n = Integer.parseInt(file.readLine());
        arr = new TreeSet<>();
        for (int i=0; i<n; i++){
            int curr = Integer.parseInt(file.readLine());
            arr.add(new Node(curr, i));
            max = Math.max(max, curr);
        }
        for (Node i : arr){
            findMultiples(i.val, i.id);
        }
        Collections.sort(edges);
        DSU dsu = new DSU(n);
        int ans = 0;
        for (Edge curr : edges){
            if (dsu.find(curr.from) != dsu.find(curr.to)){
                dsu.union(curr.from, curr.to);
                ans += curr.cost;
            }
        }
        System.out.println(ans);
    }
    public static void findMultiples(int base, int start){
        int k = 1;
        while (base*k <= max){
            if (k == 1){
                Node higher = arr.higher(new Node(base*k, -1));
                if (higher != null){
                    edges.add(new Edge(start, higher.id, higher.val % base)); 
                }
            }else{
                Node ceil = arr.ceiling(new Node(base*k, -1));
                if (ceil != null){
                    edges.add(new Edge(start, ceil.id, ceil.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 time Memory Grader output
1 Incorrect 182 ms 11312 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 173 ms 11368 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 244 ms 12036 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1464 ms 60760 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 594 ms 20404 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2584 ms 130728 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 851 ms 31320 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1828 ms 74028 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1709 ms 96972 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 713 ms 21204 KB Output isn't correct
2 Halted 0 ms 0 KB -