Submission #481730

# Submission time Handle Problem Language Result Execution time Memory
481730 2021-10-21T13:31:39 Z Johann Sirni (COCI17_sirni) C++14
42 / 140
5000 ms 786436 KB
// https://oj.uz/problem/view/COCI17_sirni
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define vi vector<int>
#define vb vector<bool>
#define pii pair<int, int>
#define vpii vector<pii>
#define vvpii vector<vpii>

struct DSU {
    vi e;
    void init(int n) { e.assign(n, -1); }
    int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
    bool connected(int a, int b) { return get(a) == get(b); }
    bool unite(int a, int b) {
        a = get(a), b = get(b); if (a == b) return 0;
        if (e[a] > e[b]) swap(a, b);
        e[a] += e[b]; e[b] = a;
        return 1;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n;
    cin >> n;
    vi cards(n);
    for (int i = 0; i < n; i++) {
        cin >> cards[i];
    }
    sort(cards.begin(), cards.end());
    int mincard = cards[0];
    vvpii edges(mincard);
    int dist;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            dist = cards[j] % cards[i];
            if (dist < mincard) edges[dist].push_back(make_pair(i, j));
        }
    }

    DSU uf;
    uf.init(n);
    ll total = 0;
    int a, b;
    for (ll i = 0; i < mincard; i++) {
        for (pii edge : edges[i]) {
            tie(a, b) = edge;
            if (uf.unite(a, b)) total += i;
        }
    }
    cout << total << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1612 KB Output is correct
2 Correct 9 ms 4044 KB Output is correct
3 Correct 3 ms 444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 3 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 972 KB Output is correct
2 Correct 3 ms 588 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5089 ms 7920 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 478 ms 1076 KB Output is correct
2 Runtime error 2425 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5100 ms 23848 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3390 ms 4860 KB Output is correct
2 Execution timed out 5093 ms 20412 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5070 ms 9620 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5092 ms 3368 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 948 ms 3976 KB Output is correct
2 Execution timed out 5098 ms 311612 KB Time limit exceeded
3 Halted 0 ms 0 KB -