Submission #891801

# Submission time Handle Problem Language Result Execution time Memory
891801 2023-12-24T05:00:00 Z cuman Sirni (COCI17_sirni) C++14
14 / 140
5000 ms 567164 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; 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) {
        return A[a[1]] % A[a[0]] < A[b[1]] % A[b[0]];
    });

    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:58: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]
   58 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 84 ms 39512 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 600 KB Output is correct
2 Execution timed out 5097 ms 565380 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 85 ms 39548 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 252 ms 22240 KB Output is correct
2 Correct 959 ms 39096 KB Output is correct
3 Runtime error 528 ms 47520 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 51 ms 6604 KB Output is correct
2 Runtime error 544 ms 48000 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 519 ms 38068 KB Output is correct
2 Runtime error 1378 ms 105736 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 80 ms 6600 KB Output is correct
2 Correct 1391 ms 71624 KB Output is correct
3 Correct 491 ms 22712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 467 ms 57720 KB Output is correct
2 Execution timed out 5041 ms 567164 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 643 ms 111132 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 200 ms 83272 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -