Submission #1006301

# Submission time Handle Problem Language Result Execution time Memory
1006301 2024-06-23T17:14:05 Z orcslop Sirni (COCI17_sirni) C++17
140 / 140
989 ms 766628 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long; 
// #define int long long
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#define f first
#define s second
#define mkp make_pair
#define pii pair<int, int>

struct DSU {
    vector<int> e;
    DSU(int N) { e = vector<int>(N, -1); }

    int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }

    bool same_set(int a, int b) { return get(a) == get(b); }

    int size(int x) { return -e[get(x)]; }

    bool unite(int x, int y) {
        x = get(x), y = get(y);
        if (x == y) return false;
        if (e[x] > e[y]) swap(x, y);
        e[x] += e[y];
        e[y] = x;
        return true;
    }
};

int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    int n; 
    cin >> n; 
    vector<int> v(n); 
    for(int i = 0; i < n; i++){
        cin >> v[i]; 
    }
    sort(all(v)); 
    v.erase(unique(all(v)), v.end()); 
    n = sz(v); 
    vector<int> nxt(v.back() + 1, -1); 
    for(int i = 0; i < n; i++){
        nxt[v[i]] = i; 
    }
    for(int i = v.back() - 1; i >= 0; i--){
        if(nxt[i] == -1){
            nxt[i] = nxt[i + 1]; 
        }
    }
    vector<vector<pii>> links(v.back() + 1); 
    for(int i = 0; i < n - 1; i++){
        links[v[i + 1] % v[i]].pb(mkp(i, i + 1)); 
        for(int j = 2 * v[i]; j <= v.back(); j += v[i]){
            links[v[nxt[j]] % v[i]].pb(mkp(nxt[j], i)); 
        }
    }
    DSU dsu(n); 
    ll ans = 0; 
    for(int i = 0; i <= v.back(); i++){
        for(auto x : links[i]){
            if(dsu.unite(x.f, x.s)){
                ans += 1LL * i; 
            }
        }
    }
    cout << ans << '\n'; 
    return 0; 
}
# Verdict Execution time Memory Grader output
1 Correct 61 ms 274256 KB Output is correct
2 Correct 123 ms 302596 KB Output is correct
3 Correct 66 ms 273692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 673 ms 683556 KB Output is correct
3 Correct 69 ms 275028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 274256 KB Output is correct
2 Correct 61 ms 273756 KB Output is correct
3 Correct 70 ms 274520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 38024 KB Output is correct
2 Correct 84 ms 69888 KB Output is correct
3 Correct 51 ms 50748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 29784 KB Output is correct
2 Correct 63 ms 50772 KB Output is correct
3 Correct 35 ms 24388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 49876 KB Output is correct
2 Correct 94 ms 88796 KB Output is correct
3 Correct 49 ms 46764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 9424 KB Output is correct
2 Correct 115 ms 84392 KB Output is correct
3 Correct 51 ms 51512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 288080 KB Output is correct
2 Correct 708 ms 660444 KB Output is correct
3 Correct 113 ms 291488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 127 ms 292076 KB Output is correct
2 Correct 989 ms 766628 KB Output is correct
3 Correct 174 ms 348624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 276820 KB Output is correct
2 Correct 860 ms 634572 KB Output is correct
3 Correct 50 ms 53312 KB Output is correct