답안 #891807

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
891807 2023-12-24T05:09:20 Z cuman Sirni (COCI17_sirni) C++14
98 / 140
5000 ms 567168 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();

    vector<array<int,2>> edges;

    if (N > 1e3) {
        int Largest = A.back();

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

    sort(all(edges), [&](const array<int,2>& a, const array<int,2>& b) {
        auto cost = [&](int i, int j) {
            return min(A[i] % A[j], A[j] % A[i]);
        };
        return cost(a[0], a[1]) < cost(b[0], b[1]);
    });

    DSU dsu;
    dsu.initSz(N);

    long long res = 0;
    for (int i = 0; i < edges.size(); i++) {
        int u = edges[i][0];
        int v = edges[i][1];
        int w = A[v] % A[u];
        res += w * dsu.unite(u, v);
    }

    cout << res;
    return 0;
}

Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:71:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 169 ms 4808 KB Output is correct
2 Correct 140 ms 6604 KB Output is correct
3 Correct 198 ms 4812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 143 ms 5576 KB Output is correct
2 Correct 75 ms 2512 KB Output is correct
3 Correct 212 ms 6604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 206 ms 6092 KB Output is correct
2 Correct 153 ms 5320 KB Output is correct
3 Correct 202 ms 5580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 378 ms 21468 KB Output is correct
2 Correct 1485 ms 38328 KB Output is correct
3 Correct 779 ms 38172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 6352 KB Output is correct
2 Correct 868 ms 39308 KB Output is correct
3 Correct 372 ms 21116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 800 ms 37696 KB Output is correct
2 Correct 2137 ms 71544 KB Output is correct
3 Correct 667 ms 23208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 6604 KB Output is correct
2 Correct 2229 ms 71848 KB Output is correct
3 Correct 720 ms 21908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 603 ms 57488 KB Output is correct
2 Execution timed out 5096 ms 567160 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 796 ms 57276 KB Output is correct
2 Execution timed out 5032 ms 566080 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 172 ms 41800 KB Output is correct
2 Execution timed out 5041 ms 567168 KB Time limit exceeded
3 Halted 0 ms 0 KB -