Submission #856138

# Submission time Handle Problem Language Result Execution time Memory
856138 2023-10-02T17:31:49 Z vjudge1 Sirni (COCI17_sirni) C++17
42 / 140
3923 ms 199696 KB
//author: Ahmet Alp Orakci
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

struct DSU {
    int n;
    vector <int> par;
    DSU(int n) : n(n) {
        par.resize(n);
        iota(par.begin(), par.end(), 0);
    }

    inline int get(int a) {
        return par[a] = (par[a] == a ? a : get(par[a]));
    }

    bool same(int u, int v) {
        return get(u) == get(v);
    }

    bool unite(int u, int v) {
        if(same(u, v))
            return false;

        par[get(u)] = get(v);
        return true;
    }
};

#define ONLINE_JUDGE
void solve() {
    int n;
    cin >> n;

    vector <int> vec(n);
    for(int &i : vec)
        cin >> i;
        
    priority_queue <tuple <int, int, int>, vector <tuple <int, int, int>>, greater <tuple <int, int, int>>> pq;
    for(int i = 0; i < int(1E7); i++) {
        int a = rand() % n, b = rand() % n;
        pq.emplace(min(vec[a] % vec[b], vec[b] % vec[a]), a, b);
    }

    i64 res = 0;
    DSU dsu(n);
    while(!pq.empty()) {
        auto [c, u, v] = pq.top();
        pq.pop();

        if(dsu.unite(u, v)) {
            res += c;
        }
    }

    cout << res;
    
    return;
}

signed main() {
    #ifndef ONLINE_JUDGE
        freopen(".in", "r", stdin);
        freopen(".out", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int t = 1; //cin >> t;
    for(int i = 1; i <= t; i++) {
        solve();
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3846 ms 198048 KB Output is correct
2 Correct 3923 ms 199068 KB Output is correct
3 Correct 3813 ms 199072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3878 ms 198356 KB Output is correct
2 Correct 3846 ms 198800 KB Output is correct
3 Correct 3718 ms 198124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3731 ms 198108 KB Output is correct
2 Correct 3698 ms 198036 KB Output is correct
3 Correct 3814 ms 198800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 3794 ms 199312 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3802 ms 197780 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3888 ms 198040 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3769 ms 199696 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3859 ms 199568 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3763 ms 198576 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3761 ms 198660 KB Output isn't correct
2 Halted 0 ms 0 KB -