답안 #1016636

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1016636 2024-07-08T09:35:11 Z daoda Sirni (COCI17_sirni) C++17
140 / 140
1534 ms 748036 KB
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FOR(x, a, b) for(int x=a;x < int(b); x++)
#define endl '\n'
#define TESTCASE ll t; cin >> t; for(ll T=0; T < t; T++)

using namespace std;
// using namespace __gnu_pbds;

typedef unsigned long long int ull;
typedef long long int ll;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<short> vs;
typedef long double ld;

// template <class T>
// using Tree = 
    // tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll INF = ll(1e18) + 10;
const ll INIT = 7;
const ll MAX_VAL = 5000;
const ll MAX_SZ = (ll) 1e4;
const ll MAX_N = 1e5;
const ll MAX_P = 1e7;
const long double eps = 1e-4;
const ll MOD = ll(1e9) + 7;

vi rd = {0, 1, 0, -1}, cd = {1, 0, -1, 0};

ll add(ll a, ll b) {
    return ((a % MOD) + (b % MOD) + 2ll * MOD) % MOD;
}

ll mult(ll a, ll b) {
    return (((a % MOD) * (b % MOD)) + MOD) % MOD;
}

ll lcm(ll a, ll b) {
    return (a * b) / gcd(a, b);
}

struct DSU {
    vi ids, comp_sz;

    DSU(int sz) {
        ids.resize(sz + 1);
        comp_sz.resize(sz + 1, 1);

        FOR(ind, 1, sz + 1) ids[ind] = ind;
    }

    int get(int x) { return ((x == ids[x]) ? x : (ids[x] = get(ids[x]))); }
    int f_sz(int x) { return comp_sz[get(x)]; }
    bool unite(int a, int b) {
        int id_sm = get(a), id_lg = get(b);

        if(id_sm == id_lg) return false;

        if(comp_sz[id_sm] > comp_sz[id_lg]) swap(id_sm, id_lg);

        comp_sz[id_lg] += comp_sz[id_sm];
        ids[id_sm] = id_lg;

        return true;
    }

};

DSU dsu(MAX_N + 1);
vpi edge[(MAX_P / 2) + 1];
int closest[MAX_P + 2], dist[MAX_P + 2];

int main() { 

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

    fast

    // TESTCASE {
        
    // }

    int n;
    cin >> n;

    int unused_id = 1;

    FOR(i, 0, n) {
        int num;
        cin >> num;

        closest[num] = unused_id;
        unused_id++;
    }

    if(closest[1]) {
        cout << 0;
        return 0;
    }

    for(int i = MAX_P - 1, cur_dist = 1; i >= 1; i--, cur_dist++) {
        if(closest[i]) cur_dist = 0;

        closest[i] = closest[i + cur_dist];
        dist[i] = cur_dist;
    }

    FOR(i, 2, MAX_P + 1) {
        if(closest[i] && !dist[i]) {
            if(closest[i + 1]) edge[dist[i + 1] + 1].push_back(make_pair(closest[i + 1], closest[i]));

            for(int j = 2*i; j <= MAX_P; j += i) {
                if(!closest[j]) break;
                edge[dist[j]].push_back(make_pair(closest[j], closest[i]));

                // cout << j << " " << dist[j] << endl;
            }
        }
    }

    ll ans = 0;

    FOR(i, 0, ((MAX_P / 2) + 1)) {
        for(auto [a, b] : edge[i]) {
            if(dsu.unite(a, b)) ans += i;
            // cout << a << " " << b << " " << i << endl;
        }
    }

    cout << ans;
}    
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 197200 KB Output is correct
2 Correct 191 ms 229460 KB Output is correct
3 Correct 87 ms 197200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 196816 KB Output is correct
2 Correct 1534 ms 748036 KB Output is correct
3 Correct 86 ms 197984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 196908 KB Output is correct
2 Correct 90 ms 196836 KB Output is correct
3 Correct 108 ms 196948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 107 ms 206488 KB Output is correct
2 Correct 152 ms 235976 KB Output is correct
3 Correct 116 ms 218924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 198744 KB Output is correct
2 Correct 121 ms 220428 KB Output is correct
3 Correct 104 ms 206612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 219304 KB Output is correct
2 Correct 177 ms 254252 KB Output is correct
3 Correct 115 ms 215088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 200272 KB Output is correct
2 Correct 180 ms 257584 KB Output is correct
3 Correct 115 ms 218092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 132 ms 210140 KB Output is correct
2 Correct 863 ms 555708 KB Output is correct
3 Correct 133 ms 213200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 131 ms 214712 KB Output is correct
2 Correct 1090 ms 719076 KB Output is correct
3 Correct 227 ms 267692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 198996 KB Output is correct
2 Correct 1008 ms 578668 KB Output is correct
3 Correct 134 ms 217532 KB Output is correct