답안 #891808

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
891808 2023-12-24T05:11:33 Z cuman Sirni (COCI17_sirni) C++14
140 / 140
1522 ms 765092 KB
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
using i64 = long long;
#define rep(i,l,r) for (int i = (l); i <= (r); i++)
 
struct DSU {
    vector<int> e;
    void initSz(int n) {
        e = vector<int>(n, -1);
    }
    int fr(int x) { return e[x] < 0 ? x : e[x] = fr(e[x]); }
    bool unite(int x, int y) {
        x = fr(x), y = fr(y);
        if (x==y) return false;
        if (e[x] > e[y]) swap(x,y);
        e[x] += e[y];
        e[y] = x;
        return true;
    }
};

signed main() {
    int N; cin >> N;
    vector<int> A(N);
    for (int& i : A) cin >> i;

    sort(all(A));
    A.erase(unique(all(A)), A.end());
    N = A.size();

    int Largest = A.back();
    vector<vector<pair<int,int>>> edges(Largest + 1);

    if (N > 1e3) {
        vector<int> lb(Largest+1);
        for (int i = 0; i <= Largest; i++) {
            lb[i] = lower_bound(A.begin() + lb[i-1], A.end(), i) - A.begin();
        }

        for (int i = 0; i < N-1; i++) {
            edges[A[i+1] % A[i]].push_back({i, i+1});
            for (int at = 2 * A[i]; at <= Largest; at += A[i]) {
                int j = lb[at];
                if (j < N) {
                    edges[A[j] % A[i]].push_back({i, j});
                }
            }
        }
    }
    else {
        for (int i = 0; i < N; i++) {
            for (int j = i+1; j < N; j++) {
                edges[A[j] % A[i]].push_back({i, j});
            }
        }
    }

    DSU dsu;
    dsu.initSz(N);

    long long res = 0;
    for (int w = 0; w <= Largest; w++) {
        for (auto item : edges[w]) {
            int u = item.first, v = item.second;
            res += w * dsu.unite(u, v);
        }
    }

    cout << res;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 142 ms 247376 KB Output is correct
2 Correct 99 ms 240796 KB Output is correct
3 Correct 156 ms 247720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 5976 KB Output is correct
2 Correct 74 ms 237396 KB Output is correct
3 Correct 157 ms 249140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 154 ms 249168 KB Output is correct
2 Correct 131 ms 244948 KB Output is correct
3 Correct 154 ms 249168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 37488 KB Output is correct
2 Correct 115 ms 67440 KB Output is correct
3 Correct 88 ms 48768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 29672 KB Output is correct
2 Correct 85 ms 50908 KB Output is correct
3 Correct 57 ms 23972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 49952 KB Output is correct
2 Correct 147 ms 86440 KB Output is correct
3 Correct 92 ms 47752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 9408 KB Output is correct
2 Correct 147 ms 85364 KB Output is correct
3 Correct 92 ms 51160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 266 ms 287568 KB Output is correct
2 Correct 1044 ms 654288 KB Output is correct
3 Correct 288 ms 290340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 270 ms 292084 KB Output is correct
2 Correct 1522 ms 765092 KB Output is correct
3 Correct 358 ms 349612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 194 ms 276776 KB Output is correct
2 Correct 1491 ms 635396 KB Output is correct
3 Correct 83 ms 51892 KB Output is correct