Submission #515357

# Submission time Handle Problem Language Result Execution time Memory
515357 2022-01-19T06:27:24 Z ronnith Sirni (COCI17_sirni) C++14
140 / 140
3575 ms 574152 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 a[N];
int nxt[Mx + 1];
vector<pair<int, int>> edges[Mx + 1];

void solve() {
    cin >> n;
    for (int i = 0; i < n;i ++) {
        cin >> a[i];
    }
    sort(a, a + n);
    int ptr = 1;
    for (int i = 1; i < n;i  ++) {
        if (a[i] != a[i - 1]) {
            a[ptr] = a[i];
            ptr ++;
        }
    }
    n = ptr;
    for (int i = 0; i < n; i ++) {
        nxt[a[i]] = -(i + 1);
    }
    for (int i = a[n - 1] - 1; i >= 0; i --) {
        if (nxt[i] == 0) {
            nxt[i] = nxt[i + 1];
            if (nxt[i] < 0) nxt[i] *= -1;
        }
    }
    for (int i = 0; i < n - 1; i ++) {
        for (int j = a[i]; j <= a[n - 1]; j += a[i]) {
            int nt;
            if (j == a[i]) {
                nt = (nxt[a[i] + 1] < 0 ? -nxt[a[i] + 1] : nxt[a[i] + 1]);
            } else {
                nt = (nxt[j] < 0 ? -nxt[j] : nxt[j]);

            }
            if (a[nt - 1] >= j + a[i]) continue;
            edges[a[nt - 1] - j].push_back(make_pair(i + 1, nt));
        }
    }
    int64_t ans = 0;
    DSU d(n);
    for (int i = 0; i < a[n - 1]; i ++) {
        for (auto& e : edges[i]) {
            if (!d.same(e.first - 1, e.second - 1)) {
                d.unite(e.first - 1, e.second - 1);
                ans += i;
            }
        }
    }
    cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 196 ms 274348 KB Output is correct
2 Correct 236 ms 276580 KB Output is correct
3 Correct 195 ms 274340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 235248 KB Output is correct
2 Correct 506 ms 275008 KB Output is correct
3 Correct 188 ms 274480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 196 ms 274376 KB Output is correct
2 Correct 205 ms 274336 KB Output is correct
3 Correct 185 ms 274372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 178 ms 249344 KB Output is correct
2 Correct 270 ms 276444 KB Output is correct
3 Correct 193 ms 254440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 146 ms 240852 KB Output is correct
2 Correct 247 ms 261644 KB Output is correct
3 Correct 183 ms 247328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 243 ms 261564 KB Output is correct
2 Correct 318 ms 291200 KB Output is correct
3 Correct 195 ms 252988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 239560 KB Output is correct
2 Correct 303 ms 285884 KB Output is correct
3 Correct 190 ms 252400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 351 ms 287292 KB Output is correct
2 Correct 1538 ms 483624 KB Output is correct
3 Correct 329 ms 290200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 314 ms 287284 KB Output is correct
2 Correct 3187 ms 574152 KB Output is correct
3 Correct 363 ms 294004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 241 ms 276800 KB Output is correct
2 Correct 3575 ms 570076 KB Output is correct
3 Correct 202 ms 254584 KB Output is correct