Submission #891788

# Submission time Handle Problem Language Result Execution time Memory
891788 2023-12-24T04:00:39 Z cuman Sirni (COCI17_sirni) C++14
98 / 140
1437 ms 199320 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 (i64 i = (l); i <= (r); i++)

const i64 MaxN = 1e5 + 2;
const i64 MaxA = 1e6 + 5;
i64 N, A[MaxN];

struct DSU {
    i64 e[MaxN];
    void initSz(i64 n) {
        memset(e, -1, n * sizeof(i64));
    }
    i64 fr(i64 x) { return e[x] < 0 ? x : e[x] = fr(e[x]); }
    bool unite(i64 x, i64 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<i64,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 (i64 i = 0; i < edges.size(); i++) {
        i64 w = edges[i][0];
        i64 u = edges[i][1];
        i64 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<i64,3>> edges;

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

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

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

    cout << res;
}

signed main() {
    cin >> N;
    for (i64 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: 'i64' {aka 'long long int'} and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     for (i64 i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
sirni.cpp: In function 'void sub2()':
sirni.cpp:75:23: warning: comparison of integer expressions of different signedness: 'i64' {aka 'long long int'} and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |     for (i64 i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 49 ms 13512 KB Output is correct
2 Correct 50 ms 13256 KB Output is correct
3 Correct 52 ms 13704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 14024 KB Output is correct
2 Correct 35 ms 8652 KB Output is correct
3 Correct 58 ms 13796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 14188 KB Output is correct
2 Correct 43 ms 13356 KB Output is correct
3 Correct 56 ms 14280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 282 ms 51088 KB Output is correct
2 Correct 1036 ms 100240 KB Output is correct
3 Correct 480 ms 101804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 8396 KB Output is correct
2 Correct 464 ms 101432 KB Output is correct
3 Correct 312 ms 51320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 619 ms 100508 KB Output is correct
2 Correct 1437 ms 198616 KB Output is correct
3 Correct 420 ms 51976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 107 ms 13748 KB Output is correct
2 Correct 1332 ms 199320 KB Output is correct
3 Correct 418 ms 51112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 51 ms 5432 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 57 ms 8812 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 1112 KB Output isn't correct
2 Halted 0 ms 0 KB -