Submission #881888

# Submission time Handle Problem Language Result Execution time Memory
881888 2023-12-02T07:54:40 Z MisterReaper Sirni (COCI17_sirni) C++17
0 / 140
1435 ms 786432 KB
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

const int MAX = 2E7;
const int MAXN = 1E5 + 5;

int par[MAXN];
struct DSU {
    int n;
    DSU(int n) : n(n) {
        iota(par, par + n, 0);
    }

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

    bool same(int a, int b) {
        return get(a) == get(b);
    }

    bool unite(int a, int b) {
        if(same(a, b)) {
            return false;
        }

        a = get(a), b = get(b);
        par[b] = a;
        return true;
    }
};

int cost(int a, int b) {
    return min(a % b, b % a);
}

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

    vector <int> p(n);
    for(int &i : p) {
        cin >> i;
    }

    p.erase(unique(p.begin(), p.end()), p.end());

    n = int(p.size());

    using T = tuple <int, int, int>;
    vector <T> edges;
    for(int i = 0; i < n; i++) {
        for(int j = p[i]; j <= MAX; j += p[i]) {
            int idx = upper_bound(p.begin(), p.end(), j) - p.begin();
            if(idx != 0) {
                edges.emplace_back(i, idx -1, cost(p[idx -1], p[i]));
            }
            if(idx != n) {
                edges.emplace_back(i, idx, cost(p[idx], p[i]));
            }
        }
    }

    sort(edges.begin(), edges.end(), [&](const T &a, const T &b) -> bool {
        return get <2> (a) < get <2> (b);
    });

    DSU dsu(n);
    i64 ans = 0;
    for(auto [u, v, c] : edges) {
        if(dsu.unite(u, v)) {
            ans += c;
        }
    }

    cout << ans;
    
    return;
}

signed main() {
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "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 Incorrect 1 ms 792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 922 ms 394352 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1435 ms 395600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 194 ms 50856 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1322 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 989 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 287 ms 51756 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 356 ms 100872 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 43 ms 14268 KB Output isn't correct
2 Halted 0 ms 0 KB -