Submission #743618

# Submission time Handle Problem Language Result Execution time Memory
743618 2023-05-17T14:43:32 Z hafo Sirni (COCI17_sirni) C++14
140 / 140
3399 ms 575072 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 2e5 + 7;
const ll oo = 1e18 + 69;
const int N = 1e7 + 7;

int n, near[N];
vector<pa> g[N];
vector<int> val;

struct DSu {
    int p[maxn];

    void init() {
        fill_n(p, n + 1, -1);
    }

    int find_node(int u) {
        return p[u] < 0 ? u:p[u] = find_node(p[u]);
    }

    bool Union(int u, int v) {
        u = find_node(u);
        v = find_node(v);
        if(u == v) return 0;
        if(p[u] > p[v]) swap(u, v);
        p[u] += p[v];
        p[v] = u;
        return 1;
    }

    void mst() {
        init();
        ll ans = 0;
        for(int i = 0; i < val.back(); i++)
            for(auto e:g[i])
                if(Union(e.fi, e.se)) ans += i;
        cout<<ans;
    }

} ds;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);

    cin>>n;
    for(int i = 0; i < n; i++) {
        int x;
        cin>>x;
        val.pb(x);
    }
    sort(all(val));
    val.erase(unique(all(val)), val.end());
    int j = 0;
    for(int i = 1; i <= val.back(); i++) {
        while(j < (int) val.size() && val[j] < i) j++;
        near[i] = j;
    }

    for(int i = 0; i < (int) val.size() - 1; i++) {
        for(int j = val[i]; j <= val.back(); j += val[i]) {
            int id;
            if(j != val[i]) id = near[j];
            else id = near[j + 1];
            if(val[id] >= j + val[i]) continue;

            if(j != val[i]) {
                g[val[id] % j].pb({i, id});
            }
            else g[val[id] % j].pb({i, id});
        }
    }
    ds.mst();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 144 ms 274400 KB Output is correct
2 Correct 177 ms 276728 KB Output is correct
3 Correct 156 ms 274320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 235204 KB Output is correct
2 Correct 344 ms 274980 KB Output is correct
3 Correct 164 ms 274508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 274516 KB Output is correct
2 Correct 147 ms 274256 KB Output is correct
3 Correct 156 ms 274400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 165 ms 249400 KB Output is correct
2 Correct 268 ms 276100 KB Output is correct
3 Correct 169 ms 254604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 240808 KB Output is correct
2 Correct 243 ms 261772 KB Output is correct
3 Correct 156 ms 247508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 306 ms 261420 KB Output is correct
2 Correct 284 ms 291072 KB Output is correct
3 Correct 183 ms 253004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 239592 KB Output is correct
2 Correct 278 ms 285584 KB Output is correct
3 Correct 176 ms 252588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 260 ms 287404 KB Output is correct
2 Correct 1414 ms 482776 KB Output is correct
3 Correct 313 ms 289788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 279 ms 287840 KB Output is correct
2 Correct 2850 ms 575072 KB Output is correct
3 Correct 280 ms 293984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 176 ms 276784 KB Output is correct
2 Correct 3399 ms 570168 KB Output is correct
3 Correct 173 ms 254800 KB Output is correct