답안 #891786

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

const int MaxN = 1e5;
const int MaxA = 1e6 + 1;
int N, A[MaxN];

struct DSU {
    int e[MaxN];
    void initSz(int n) {
        memset(e, -1, n * sizeof(int));
    }
    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;
    }
};

DSU dsu;

void sub1() {
    sort(A, A + N);
    N = unique(A, A + N) - A;

    vector<array<int,3>> edges;

    rep(i, 0, N-1) {
        rep(j, i+1, N-1) {
            edges.push_back({A[j] % A[i], i, j});
        }
    }

    sort(all(edges));
    dsu.initSz(N);

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

    cout << res;
}

void sub2() {
    sort(A, A + N);
    N = unique(A, A + N) - A;

    vector<array<int,3>> edges;

    for (int i = 0; i < N; i++) {
        for (int mul = A[i]; mul <= MaxA; mul += A[i]) {
            int j = mul == A[i] ? upper_bound(A, A + N, mul) - A : lower_bound(A, A + N, mul) - A;
            if (j < N) {
                edges.push_back({A[j] % A[i], i, j});
            }
        }
    }

    sort(all(edges));
    dsu.initSz(N);

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

    cout << res;
}

signed main() {
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }

    if (N <= 2e3) {
        sub1();
    }
    else {
        sub2();
    }

    return 0;
}

Compilation message

sirni.cpp: In function 'void sub1()':
sirni.cpp:45:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
sirni.cpp: In function 'void sub2()':
sirni.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 7116 KB Output is correct
2 Correct 47 ms 8652 KB Output is correct
3 Correct 50 ms 8392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 6604 KB Output is correct
2 Correct 32 ms 3536 KB Output is correct
3 Correct 52 ms 7880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 8136 KB Output is correct
2 Correct 41 ms 7628 KB Output is correct
3 Correct 50 ms 6856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 263 ms 27052 KB Output is correct
2 Correct 977 ms 51864 KB Output is correct
3 Correct 445 ms 51808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 4304 KB Output is correct
2 Correct 441 ms 50792 KB Output is correct
3 Correct 277 ms 26800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 562 ms 50856 KB Output is correct
2 Correct 1340 ms 101504 KB Output is correct
3 Correct 401 ms 27984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 7352 KB Output is correct
2 Correct 1294 ms 100492 KB Output is correct
3 Correct 400 ms 26556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 50 ms 2528 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 57 ms 3784 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 792 KB Output isn't correct
2 Halted 0 ms 0 KB -