Submission #968659

# Submission time Handle Problem Language Result Execution time Memory
968659 2024-04-23T19:52:44 Z RandomUser Sirni (COCI17_sirni) C++17
140 / 140
4051 ms 401040 KB
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
 
#pragma GCC optimize("O3,unroll-loops")
 
using ll = long long;
 
struct DSU {
    int n, comp;
    vector<int> par, size;
 
    void config(int _n) {
        n = _n + 1;
        comp = _n;
        par.resize(n+1);
        size.resize(n+1, 1);
        for(int i=1; i<=n; i++) par[i] = i;
    }
 
    int get(int u) {
        if(u == par[u]) return u;
        return par[u] = get(par[u]);
    }
 
    bool uni(int u, int v) {
        u = get(u), v = get(v);
 
        if(u == v) return 0;
        if(size[u] < size[v]) swap(u, v);
 
        size[u] += size[v];
        par[v] = u;
        comp--;
 
        return 1;
    }
};
 
struct Edge {
    int a, b, w;
    bool operator<(Edge &e) { return w < e.w; }
};
 
int32_t main() {
    ios_base::sync_with_stdio(false);
    cout.tie(0); cin.tie(0);
 
    int n;
    ll ans = 0;
    cin >> n;
 
    vector<int> v(n);
    for(int &x : v) cin >> x;
    set<int> s;
    for(int &x : v) s.insert(x);
    v = vector<int>(s.begin(), s.end());
 
    if(v[0] == 1 || v.size() == 1) {
        cout << 0 << '\n';
        return 0;
    }
 
    DSU dsu; dsu.config(n);
    vector<Edge> edges;
 
    n = v.size();
    int mx = v[n-1];
    for(int i=0; i<n; i++) {
        int j = i;
        for(int val=v[i]; val<=mx; val+=v[i]) {
            int last = j;
            while(j + 1 < n && v[j] < val) j++;
            if(v[j] == v[i]) j++;
            if(j != n && last != j) edges.push_back({ i, j, v[j] % v[i] });
        }
    }
 
    sort(edges.begin(), edges.end());
    for(Edge &e : edges)
        if(dsu.uni(e.a, e.b)) ans += e.w;
    cout << ans << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 22 ms 3536 KB Output is correct
3 Correct 2 ms 792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 85 ms 1288 KB Output is correct
3 Correct 2 ms 920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 2 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 928 ms 20032 KB Output is correct
2 Correct 1068 ms 56820 KB Output is correct
3 Correct 690 ms 31288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 5132 KB Output is correct
2 Correct 222 ms 52756 KB Output is correct
3 Correct 680 ms 18964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1051 ms 56456 KB Output is correct
2 Correct 1244 ms 107056 KB Output is correct
3 Correct 928 ms 32728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 144 ms 10928 KB Output is correct
2 Correct 1147 ms 104756 KB Output is correct
3 Correct 757 ms 31908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1029 ms 33292 KB Output is correct
2 Correct 3182 ms 401040 KB Output is correct
3 Correct 1067 ms 33456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1020 ms 31792 KB Output is correct
2 Correct 4051 ms 400300 KB Output is correct
3 Correct 1042 ms 32060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 4864 KB Output is correct
2 Correct 3993 ms 400404 KB Output is correct
3 Correct 810 ms 32820 KB Output is correct