답안 #667510

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
667510 2022-12-01T15:52:48 Z tanprodium Sirni (COCI17_sirni) C++14
140 / 140
3112 ms 574404 KB
#include<bits/stdc++.h>
#define fi first
#define se second

using namespace std;

using pii = pair<int, int>;
using ll = long long;

const int N = 1e5;
const int V = 1e7;

int a[N + 5];
int par[N + 5];
int id[V + 5];
vector<int> cpr;
vector<pii> e[V + 5];

int getpar(int u) {
    while (par[u] > 0) u = par[u];
    return (u);
}

bool uni(int u, int v) {
    u = getpar(u), v = getpar(v);

    if (u == v) return (false);

    if (par[u] > par[v]) swap(u, v);

    par[u] += par[v];
    par[v] = u;

    return (true);
}

void solve() {
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        cpr.push_back(a[i]);
    }

    sort(cpr.begin(), cpr.end());
    cpr.resize(unique(cpr.begin(), cpr.end()) - cpr.begin());

    n = (int)cpr.size();

    vector<int> vec;

    for (int i = 1; i <= n; i++) {
        par[i] = -1;
        a[i] = cpr[i - 1];
        id[a[i]] = i;
        vec.push_back(a[i]);
    }

    for (int i = 1; i <= n; i++) {
        int val = a[i];
        int d = 1;

        while (true) {
            int take = max(val + 1, val * d);;
            auto iter = lower_bound(vec.begin(), vec.end(), take);
            if (iter == vec.end()) break;
            int nval = *iter;
            int j = id[nval];

            int dist = nval % val;
            e[dist].push_back({i, j});

            d = nval / val + 1;
        }
    }

    ll ans = 0;

    for (int i = 0; i <= V; i++) {
        for (pii x : e[i]) {
            int u = x.fi, v = x.se;

            if (uni(u, v)) ans += 1ll * i;
        }
    }

    cout << ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 238856 KB Output is correct
2 Correct 133 ms 239656 KB Output is correct
3 Correct 123 ms 239400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 128 ms 235332 KB Output is correct
2 Correct 127 ms 236404 KB Output is correct
3 Correct 127 ms 239240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 133 ms 239240 KB Output is correct
2 Correct 133 ms 237596 KB Output is correct
3 Correct 130 ms 239156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 249 ms 250588 KB Output is correct
2 Correct 526 ms 277284 KB Output is correct
3 Correct 291 ms 255528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 240940 KB Output is correct
2 Correct 289 ms 261544 KB Output is correct
3 Correct 248 ms 248464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 392 ms 262752 KB Output is correct
2 Correct 624 ms 292460 KB Output is correct
3 Correct 277 ms 254060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 165 ms 239932 KB Output is correct
2 Correct 603 ms 286920 KB Output is correct
3 Correct 253 ms 253400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 296 ms 288384 KB Output is correct
2 Correct 2287 ms 483812 KB Output is correct
3 Correct 320 ms 290852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 311 ms 288668 KB Output is correct
2 Correct 3112 ms 574404 KB Output is correct
3 Correct 421 ms 294752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 158 ms 271848 KB Output is correct
2 Correct 2884 ms 562892 KB Output is correct
3 Correct 297 ms 255340 KB Output is correct