답안 #881896

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
881896 2023-12-02T08:07:43 Z MisterReaper Sirni (COCI17_sirni) C++17
140 / 140
4351 ms 396940 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;
    }

    sort(p.begin(), p.end());
    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 +1 < n; i++) {
        for(int j = p[i]; j <= MAX; j += p[i]) {
            int idx = lower_bound(p.begin() + i +1, p.end(), j) - p.begin();
            if(idx == n)
                break;

            if(j <= p[idx] && p[idx] <= j + p[i]) {
                edges.emplace_back(i, idx, cost(p[i], p[idx]));
            }
        }
    }

    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) {
        //cerr << u << " " << v << " " << c << "\n";
        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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 59 ms 4088 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 441 ms 1476 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 600 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 137 ms 13216 KB Output is correct
2 Correct 467 ms 50672 KB Output is correct
3 Correct 196 ms 25524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 3536 KB Output is correct
2 Correct 248 ms 50544 KB Output is correct
3 Correct 130 ms 13492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 302 ms 51384 KB Output is correct
2 Correct 624 ms 99592 KB Output is correct
3 Correct 181 ms 25776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 7924 KB Output is correct
2 Correct 582 ms 100256 KB Output is correct
3 Correct 179 ms 25528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 175 ms 26548 KB Output is correct
2 Correct 3025 ms 396492 KB Output is correct
3 Correct 202 ms 27560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 177 ms 26540 KB Output is correct
2 Correct 4351 ms 396764 KB Output is correct
3 Correct 346 ms 27304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 4812 KB Output is correct
2 Correct 4122 ms 396940 KB Output is correct
3 Correct 201 ms 27992 KB Output is correct