Submission #915465

# Submission time Handle Problem Language Result Execution time Memory
915465 2024-01-24T02:47:37 Z Em1L Sirni (COCI17_sirni) C++17
140 / 140
2074 ms 746532 KB
#include <bits/stdc++.h>
using namespace std;

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

class DSU {
 private:
    vector < int > parent, scale;

    inline int GetRoot(int v) {
        return parent[v] = v == parent[v] ? v : GetRoot(parent[v]);
    }

 public:
    DSU(int n) {
        parent.resize(n);
        iota(parent.begin(), parent.end(), 0);
        scale.resize(n, 1);
    }

    inline bool Unite(int v, int u) {
        v = GetRoot(v), u = GetRoot(u);

        if (v == u)
            return false;

        if (scale[v] < scale[u])
            swap(v, u);

        parent[u] = v;
        scale[v] += scale[u];

        return true;
    }
};

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);

    int n; cin >> n;

    vector < int > p(n);

    for (int i = 0; i < n; i++)
        cin >> p[i];

    sort(p.begin(), p.end()); p.erase(unique(p.begin(), p.end()), p.end());

    n = p.size();
    int P = p.back();

    vector < int > nextGreater(P + 1, -1);

    for (int i = 0; i < n; i++)
        nextGreater[p[i]] = i;

    for (int i = P; i >= 1; i--)
        if (nextGreater[i] == -1)
            nextGreater[i] = nextGreater[i + 1];

    vector < vector < pii > > edge(P + 1);

    for (int i = 0; i < n - 1; i++) {
        edge[p[i + 1] % p[i]].push_back({ i, i + 1 });

        for (int x = 2 * p[i]; x <= P; x += p[i]) {
            int j = nextGreater[x];

            if (j != -1)
                edge[p[j] % p[i]].push_back({ i, j });
        }
    }

    ll ans = 0;

    DSU dsu(n);

    for (int c = 0; c <= P; c++)
        for (auto [i, j] : edge[c])
            if (dsu.Unite(i, j))
                ans += c;

    cout << ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 156 ms 274260 KB Output is correct
2 Correct 231 ms 302632 KB Output is correct
3 Correct 160 ms 273496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1152 ms 668544 KB Output is correct
3 Correct 150 ms 275028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 147 ms 274256 KB Output is correct
2 Correct 152 ms 273752 KB Output is correct
3 Correct 145 ms 274512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 39296 KB Output is correct
2 Correct 117 ms 70144 KB Output is correct
3 Correct 71 ms 51120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 29784 KB Output is correct
2 Correct 80 ms 51284 KB Output is correct
3 Correct 46 ms 25712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 50576 KB Output is correct
2 Correct 148 ms 90068 KB Output is correct
3 Correct 63 ms 49588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 9676 KB Output is correct
2 Correct 168 ms 87360 KB Output is correct
3 Correct 66 ms 51004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 288336 KB Output is correct
2 Correct 1165 ms 632644 KB Output is correct
3 Correct 215 ms 290972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 216 ms 292324 KB Output is correct
2 Correct 2074 ms 746532 KB Output is correct
3 Correct 352 ms 347804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 151 ms 276816 KB Output is correct
2 Correct 1909 ms 634152 KB Output is correct
3 Correct 71 ms 51260 KB Output is correct