답안 #514884

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
514884 2022-01-18T17:20:20 Z ronnith Sirni (COCI17_sirni) C++14
98 / 140
3672 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, p[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 >> p[i];
    }
    sort(p, p + n);
    values.push_back(p[0]);
    for (int i = 1; i < n;i  ++) {
        if (p[i] != p[i - 1]) {
            values.push_back(p[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 157 ms 313916 KB Output is correct
2 Correct 218 ms 319116 KB Output is correct
3 Correct 160 ms 313952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 272 ms 313580 KB Output is correct
2 Correct 544 ms 315004 KB Output is correct
3 Correct 168 ms 313964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 160 ms 313668 KB Output is correct
2 Correct 161 ms 313408 KB Output is correct
3 Correct 173 ms 313784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 426 ms 336272 KB Output is correct
2 Correct 945 ms 401152 KB Output is correct
3 Correct 447 ms 354044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 202 ms 318416 KB Output is correct
2 Correct 812 ms 385304 KB Output is correct
3 Correct 491 ms 337052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 685 ms 385228 KB Output is correct
2 Correct 1133 ms 464956 KB Output is correct
3 Correct 455 ms 352972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 544 ms 323308 KB Output is correct
2 Correct 1358 ms 460460 KB Output is correct
3 Correct 438 ms 352268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 301 ms 351368 KB Output is correct
2 Runtime error 1656 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 308 ms 351332 KB Output is correct
2 Runtime error 3251 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 184 ms 319056 KB Output is correct
2 Runtime error 3672 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -