Submission #891802

# Submission time Handle Problem Language Result Execution time Memory
891802 2023-12-24T05:04:07 Z cuman Sirni (COCI17_sirni) C++14
84 / 140
5000 ms 566812 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<int> lb(Largest+1);
    for (int i = 0; i <= Largest; i++) {
        lb[i] = lower_bound(all(A), i) - A.begin();
    }

    vector<array<int,2>> edges;
    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});
            }
        }
    }

    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:61: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]
   61 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 91 ms 39516 KB Output is correct
2 Correct 1152 ms 73288 KB Output is correct
3 Correct 90 ms 39760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 600 KB Output is correct
2 Execution timed out 5050 ms 565608 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 92 ms 39548 KB Output is correct
2 Correct 80 ms 39508 KB Output is correct
3 Correct 87 ms 39516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 382 ms 21748 KB Output is correct
2 Correct 1490 ms 39348 KB Output is correct
3 Correct 789 ms 38324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 6576 KB Output is correct
2 Correct 869 ms 39344 KB Output is correct
3 Correct 376 ms 19092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 804 ms 38144 KB Output is correct
2 Correct 2135 ms 71336 KB Output is correct
3 Correct 675 ms 21476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 6092 KB Output is correct
2 Correct 2229 ms 71184 KB Output is correct
3 Correct 727 ms 21744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 641 ms 58532 KB Output is correct
2 Execution timed out 5099 ms 565972 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 836 ms 58276 KB Output is correct
2 Execution timed out 5057 ms 566812 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 191 ms 41928 KB Output is correct
2 Execution timed out 5064 ms 566388 KB Time limit exceeded
3 Halted 0 ms 0 KB -