답안 #1057659

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1057659 2024-08-14T02:43:45 Z vjudge1 Sirni (COCI17_sirni) C++17
140 / 140
1362 ms 770124 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;

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();

    function<int(int)> root = [&](int x) -> int {
        if(x == fa[x]) return x;
        return fa[x] = root(fa[x]);
    };
    function<bool(int, int)> join = [&](int u, int v) -> bool {
        u = root(u); v = root(v);
        if(u == v) return false;
        fa[root(u)] = root(v);
        return true;
    };
    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 52 ms 274260 KB Output is correct
2 Correct 141 ms 302456 KB Output is correct
3 Correct 55 ms 273672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 969 ms 685488 KB Output is correct
3 Correct 60 ms 275024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 274260 KB Output is correct
2 Correct 54 ms 273868 KB Output is correct
3 Correct 56 ms 274516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 38596 KB Output is correct
2 Correct 155 ms 68680 KB Output is correct
3 Correct 75 ms 51136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 29924 KB Output is correct
2 Correct 70 ms 50900 KB Output is correct
3 Correct 51 ms 25028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 50084 KB Output is correct
2 Correct 148 ms 91672 KB Output is correct
3 Correct 67 ms 50364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 9796 KB Output is correct
2 Correct 152 ms 88452 KB Output is correct
3 Correct 63 ms 50716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 287952 KB Output is correct
2 Correct 942 ms 673324 KB Output is correct
3 Correct 131 ms 291792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 292756 KB Output is correct
2 Correct 1362 ms 770124 KB Output is correct
3 Correct 216 ms 349648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 276816 KB Output is correct
2 Correct 1112 ms 635020 KB Output is correct
3 Correct 80 ms 52924 KB Output is correct