답안 #1016617

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1016617 2024-07-08T09:15:10 Z daoda Sirni (COCI17_sirni) C++17
112 / 140
1150 ms 786432 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 + 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]));
            }
        }
    }

    ll ans = 0;

    FOR(i, 0, MAX_P + 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 133 ms 314696 KB Output is correct
2 Correct 244 ms 346960 KB Output is correct
3 Correct 134 ms 314736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 127 ms 314448 KB Output is correct
2 Runtime error 1150 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 157 ms 314244 KB Output is correct
2 Correct 185 ms 314324 KB Output is correct
3 Correct 151 ms 314448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 166 ms 324488 KB Output is correct
2 Correct 218 ms 354220 KB Output is correct
3 Correct 175 ms 337048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 141 ms 316356 KB Output is correct
2 Correct 188 ms 338492 KB Output is correct
3 Correct 166 ms 324728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 181 ms 337388 KB Output is correct
2 Correct 247 ms 372288 KB Output is correct
3 Correct 178 ms 333308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 147 ms 317752 KB Output is correct
2 Correct 251 ms 375600 KB Output is correct
3 Correct 186 ms 336040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 187 ms 328460 KB Output is correct
2 Correct 922 ms 673784 KB Output is correct
3 Correct 199 ms 331276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 221 ms 332836 KB Output is correct
2 Runtime error 904 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 154 ms 316588 KB Output is correct
2 Correct 1036 ms 696848 KB Output is correct
3 Correct 176 ms 335500 KB Output is correct