Submission #1106930

# Submission time Handle Problem Language Result Execution time Memory
1106930 2024-10-31T09:18:37 Z vjudge1 Sirni (COCI17_sirni) C++17
140 / 140
1208 ms 758416 KB
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;

template<class X, class Y>
bool minimize(X &x, const Y &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

using pii = pair<int, int>;
const int MAXN = 1e7 + 5;

namespace dsu {
    vector<int> lab;

    void init(int N) {
        lab.assign(N + 5, -1);
    }

    int root(int u) {
        if(lab[u] < 0) return u;
        return lab[u] = root(lab[u]);
    }

    bool is_same(int u, int v) {
        return root(u) == root(v);
    }

    bool join(int u, int v) {
        if(is_same(u, v)) return false;

        u = root(u);
        v = root(v);

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

        lab[u] += lab[v];
        lab[v] = u;
        return true;
    }
}

int N;
int b[MAXN + 5];
vector<pii> edges[MAXN];

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> N;
    vector<int> a(N + 1, 0);
    for(int i = 1; i <= N; i++) cin >> a[i];
    sort(a.begin() + 1, a.end());
    dsu::init(N);
    for(int i = 1; i <= N; i++) {
        int j = i;
        while(j < N and a[i] == a[j + 1]) {
            dsu::join(i, j);
            j++;
        }
        b[a[i]] = i;
        i = j;
    }
    for(int i = a[N]; i >= 1; i--)
        b[i] = (b[i] == 0 ? b[i + 1] : b[i]);
    for(int i = 1; i <= N; i++) {
        int j = i;
        while(j < N and a[i] == a[j + 1]) {
            dsu::join(i, j);
            j++;
        }
        dsu::join(i, j);
        if(b[a[i] + 1])
            edges[a[b[a[i] + 1]] % a[i]].emplace_back(i, b[a[i] + 1]);
        for(int j = 2 * a[i]; j <= a[N]; j += a[i]) {
            if(b[j])
                edges[a[b[j]] % a[i]].emplace_back(i, b[j]);
        }
        i = j;
    }
    i64 ans = 0;
    for(int i = 0; i < MAXN; i++) {
        for(auto [u, v] : edges[i])
            if(dsu::join(u, v))
                ans += i;
    }
    cout << ans;
    return (0 ^ 0);
}
# Verdict Execution time Memory Grader output
1 Correct 64 ms 274504 KB Output is correct
2 Correct 125 ms 303688 KB Output is correct
3 Correct 60 ms 274504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 236624 KB Output is correct
2 Correct 941 ms 669056 KB Output is correct
3 Correct 67 ms 275016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 274504 KB Output is correct
2 Correct 63 ms 274312 KB Output is correct
3 Correct 64 ms 274512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 107 ms 251008 KB Output is correct
2 Correct 146 ms 286192 KB Output is correct
3 Correct 111 ms 263488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 242620 KB Output is correct
2 Correct 143 ms 264008 KB Output is correct
3 Correct 90 ms 249680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 267352 KB Output is correct
2 Correct 183 ms 304736 KB Output is correct
3 Correct 102 ms 262052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 240224 KB Output is correct
2 Correct 201 ms 300040 KB Output is correct
3 Correct 109 ms 264756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 288076 KB Output is correct
2 Correct 944 ms 633812 KB Output is correct
3 Correct 128 ms 290608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 292320 KB Output is correct
2 Correct 1208 ms 758416 KB Output is correct
3 Correct 238 ms 350052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 276784 KB Output is correct
2 Correct 1135 ms 636212 KB Output is correct
3 Correct 106 ms 267060 KB Output is correct