답안 #1106834

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1106834 2024-10-31T07:38:18 Z baotoan655 Sirni (COCI17_sirni) C++14
140 / 140
1167 ms 754188 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);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 274192 KB Output is correct
2 Correct 120 ms 302408 KB Output is correct
3 Correct 67 ms 273536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 780 KB Output is correct
2 Correct 774 ms 668412 KB Output is correct
3 Correct 66 ms 275016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 274316 KB Output is correct
2 Correct 76 ms 273732 KB Output is correct
3 Correct 65 ms 274564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 38332 KB Output is correct
2 Correct 103 ms 73224 KB Output is correct
3 Correct 64 ms 50616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 29776 KB Output is correct
2 Correct 64 ms 50860 KB Output is correct
3 Correct 48 ms 25288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 54424 KB Output is correct
2 Correct 123 ms 90036 KB Output is correct
3 Correct 57 ms 49596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 9544 KB Output is correct
2 Correct 127 ms 87224 KB Output is correct
3 Correct 59 ms 51640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 287944 KB Output is correct
2 Correct 833 ms 634992 KB Output is correct
3 Correct 128 ms 292552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 122 ms 292544 KB Output is correct
2 Correct 1167 ms 754188 KB Output is correct
3 Correct 256 ms 348968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 276808 KB Output is correct
2 Correct 1042 ms 636716 KB Output is correct
3 Correct 62 ms 54452 KB Output is correct