Submission #1106837

# Submission time Handle Problem Language Result Execution time Memory
1106837 2024-10-31T07:39:52 Z vjudge1 Sirni (COCI17_sirni) C++14
140 / 140
1176 ms 758484 KB
#include <bits/stdc++.h>
#define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define REV(i, b, a) for(int i = (b); i >= (a); --i)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define ll long long
#define fi first
#define se second

using namespace std;

const int N = 1e5 + 5;
int n;
int fa[N];
vector<int> ve;

int root(int u) {
    if(u == fa[u]) return u;
    return fa[u] = root(fa[u]);
}
bool join(int u, int v) {
    u = root(u); v = root(v);
    if(u == v) return false;
    fa[u] = v;
    return true;
}

void solve(int tc) {
    // cout << "Case #" << tc << ": ";
    cin >> n;
    REP(i, n) {
        int x;
        cin >> x;
        ve.emplace_back(x);
    }
    sort(ve.begin(), ve.end());
    ve.resize(unique(ve.begin(), ve.end()) - ve.begin());
    n = ve.size();

    REP(i, n) fa[i] = i;
    int lim = ve.back();
    vector<int> pos(lim + 1, -1);
    REP(i, n) {
        pos[ve[i]] = i;
    }
    for(int i = lim - 1; i >= 0; --i) {
        if(pos[i] == -1) pos[i] = pos[i + 1];
    }

    vector<vector<pair<int, int>>> edge(lim);

    REP(i, n - 1) {
        edge[ve[i + 1] % ve[i]].emplace_back(i, i + 1);
        for(int j = 2 * ve[i]; j <= lim; j += ve[i]) {
            int nxt = pos[j];
            edge[ve[nxt] % ve[i]].emplace_back(i, nxt);
        }
    }
    int ans = 0;
    FOR(i, 0, lim - 1) {
        for(auto e : edge[i]) {
            if(join(e.fi, e.se)) {
                ans += i;
            }
        }
    }
    cout << ans << '\n';
    return;
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int tc = 1;
    // cin >> tc;
    for(int i = 1; i <= tc; ++i) solve(i);
    return (0);
}
# Verdict Execution time Memory Grader output
1 Correct 67 ms 274248 KB Output is correct
2 Correct 138 ms 302408 KB Output is correct
3 Correct 72 ms 273516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 832 ms 669952 KB Output is correct
3 Correct 78 ms 275016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 274368 KB Output is correct
2 Correct 62 ms 273912 KB Output is correct
3 Correct 66 ms 274516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 38360 KB Output is correct
2 Correct 116 ms 73184 KB Output is correct
3 Correct 67 ms 50608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 29776 KB Output is correct
2 Correct 70 ms 50956 KB Output is correct
3 Correct 47 ms 25268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 54432 KB Output is correct
2 Correct 131 ms 89180 KB Output is correct
3 Correct 65 ms 49588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 9528 KB Output is correct
2 Correct 125 ms 88936 KB Output is correct
3 Correct 61 ms 51896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 141 ms 287812 KB Output is correct
2 Correct 867 ms 638244 KB Output is correct
3 Correct 131 ms 291640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 292660 KB Output is correct
2 Correct 1176 ms 758484 KB Output is correct
3 Correct 196 ms 350244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 276808 KB Output is correct
2 Correct 1050 ms 636512 KB Output is correct
3 Correct 66 ms 54040 KB Output is correct