Submission #1106835

# Submission time Handle Problem Language Result Execution time Memory
1106835 2024-10-31T07:38:43 Z vjudge1 Sirni (COCI17_sirni) C++14
140 / 140
1144 ms 754584 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 64 ms 274248 KB Output is correct
2 Correct 143 ms 302408 KB Output is correct
3 Correct 67 ms 273484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 848 KB Output is correct
2 Correct 839 ms 672044 KB Output is correct
3 Correct 68 ms 275008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 274268 KB Output is correct
2 Correct 63 ms 273744 KB Output is correct
3 Correct 75 ms 274504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 38324 KB Output is correct
2 Correct 103 ms 76028 KB Output is correct
3 Correct 65 ms 50800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 29776 KB Output is correct
2 Correct 83 ms 50908 KB Output is correct
3 Correct 49 ms 25444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 54424 KB Output is correct
2 Correct 134 ms 95368 KB Output is correct
3 Correct 58 ms 49588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 9456 KB Output is correct
2 Correct 144 ms 87368 KB Output is correct
3 Correct 66 ms 52664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 149 ms 287944 KB Output is correct
2 Correct 907 ms 634148 KB Output is correct
3 Correct 135 ms 291640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 292664 KB Output is correct
2 Correct 1144 ms 754584 KB Output is correct
3 Correct 206 ms 350780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 276808 KB Output is correct
2 Correct 977 ms 636556 KB Output is correct
3 Correct 66 ms 53448 KB Output is correct