답안 #514890

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
514890 2022-01-18T17:24:30 Z ronnith Sirni (COCI17_sirni) C++14
98 / 140
3885 ms 786436 KB
#include <bits/stdc++.h>

#define index ind

using namespace std;

struct DSU {
    int cnt;
    int size;
    vector<int> e;
    DSU (int _) : size (_) {
        e = vector<int> (size, -1);
    }
    int root(int x) {
        return (e[x] < 0 ? x : (e[x] = root(e[x])));
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    void unite(int x, int y) {
        if (same(x, y)) return;
        x = root(x), y = root(y);
        if (-e[x] > -e[y]) {
            swap(x, y);
        }
        e[y] += e[x];
        e[x] = y;
        cnt --;
    }
};

const int N = (int)1e5;
const int Mx = (int)1e7;
int n;
int nxt[Mx + 1];
int index[Mx + 1];
vector<int> values;
vector<pair<int, int>> edges[Mx + 1];
vector<tuple<int, int, int>> sr_edges;

void solve() {
    cin >> n;
    for (int i = 0; i < n;i ++) {
        cin >> nxt[i];
    }
    sort(nxt, nxt + n);
    values.push_back(nxt[0]);
    for (int i = 1; i < n;i  ++) {
        if (nxt[i] != nxt[i - 1]) {
            values.push_back(nxt[i]);
        }
    }
    int ptr = 0;
    for (auto& e : values) {
        nxt[e] = 1;
        index[e] = ptr;
        ptr ++;
    }
    int crr = -1;
    for (int i = Mx; i > 0; i --) {
        int prev = crr;
        if (nxt[i] == 1) crr = i;
        nxt[i] = prev;
    }
    for (auto& e : values) {
        for (int i = e; i <= Mx; i += e) {
            if (i != e && index[i] != -1) {
                edges[0].push_back(make_pair(index[e], index[i]));
            } else if (nxt[i] != -1) {
                if (i + e <= nxt[i]) continue;
                edges[nxt[i] - i].push_back(make_pair(index[e], index[nxt[i]]));
            }
        }
    }
    for (int i = 0; i < Mx + 1; i ++) {
        for (auto& e : edges[i]) {
            sr_edges.push_back(make_tuple(e.first, e.second, i));
        }
    }
    DSU d(n);
    int64_t ans = 0;
    for (auto& e : sr_edges) {
        int x, y, w; tie(x, y, w) = e;
        // cout << x + 1 << ' ' << y + 1 << ' ' << w << '\n';
        if (!d.same(x, y)) {
            d.unite(x, y);
            ans += w;
        }
    }
    cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    for (int i = 0; i < Mx + 1; i ++) {
        index[i] = -1;
    }
    solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 206 ms 313676 KB Output is correct
2 Correct 234 ms 319104 KB Output is correct
3 Correct 167 ms 313980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 254 ms 313628 KB Output is correct
2 Correct 579 ms 315060 KB Output is correct
3 Correct 161 ms 313928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 186 ms 313684 KB Output is correct
2 Correct 184 ms 313476 KB Output is correct
3 Correct 170 ms 313796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 445 ms 335876 KB Output is correct
2 Correct 1016 ms 400672 KB Output is correct
3 Correct 532 ms 353692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 226 ms 318368 KB Output is correct
2 Correct 829 ms 384844 KB Output is correct
3 Correct 545 ms 336692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 732 ms 384844 KB Output is correct
2 Correct 1216 ms 464476 KB Output is correct
3 Correct 472 ms 352556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 489 ms 323032 KB Output is correct
2 Correct 1388 ms 460172 KB Output is correct
3 Correct 501 ms 351676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 309 ms 350872 KB Output is correct
2 Runtime error 1680 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 312 ms 351148 KB Output is correct
2 Runtime error 3363 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 199 ms 318988 KB Output is correct
2 Runtime error 3885 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -