Submission #139935

# Submission time Handle Problem Language Result Execution time Memory
139935 2019-08-01T17:01:38 Z Minnakhmetov Sirni (COCI17_sirni) C++14
140 / 140
2660 ms 178636 KB
#include<bits/stdc++.h>
using namespace std;
 
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
 
const int N = 1e5 + 5, M = 1e7 + 5, INF = 1e9;
int q[N], w[M], nt[M], r_nt[M], c[M], st[N];
pair<int, int> mn[N];
bool b[M], d[M];
 
int findSet(int v) {
    return q[v] < 0 ? v : q[v] = findSet(q[v]);
}
 
bool connect(int a, int b) {
    a = findSet(a);
    b = findSet(b);
 
    if (a == b)
        return false;
 
    if (q[a] > q[b])
        swap(a, b);
    q[a] += q[b];
    q[b] = a;
 
    return true;
}
 
signed main() { 
#ifdef HOME
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        int x;
        cin >> x; 
        b[x] = 1;
    }
 
    fill(w, w + M, -1);
    fill(c, c + M, -1);
    fill(q, q + N, -1);
 
    vector<int> v;
 
    int cnt = 0;
 
    for (int i = 0; i < M; i++) {
        if (b[i]) {
            w[i] = cnt++;
        }
    }
 
    int remain = cnt;
 
    for (int i = 0; i < M; i++) {
        if (b[i]) {
            for (int j = i + i; j < M; j += i) {
                if (b[j] && connect(w[i], w[j])) {
                    remain--;
                }
                d[j] = 1;
            }
            v.push_back(i);
        }
    }
 
    for (int i = 0; i < v.size(); i++) {
        for (int j = v[i]; j < M; j += v[i]) {
            c[j] = i;
        }
    }
 
 
    nt[M - 1] = INF;
 
    for (int i = M - 2; i >= 0; i--) {
        nt[i] = b[i] ? i : nt[i + 1];
    }
 
    r_nt[0] = -1;
 
    for (int i = 1; i < M; i++) {
        r_nt[i] = c[i] != -1 ? i : r_nt[i - 1];
    }
 
    ll ans = 0;
 
    while (remain > 1) {
        for (int i = 0; i < cnt; i++)
            mn[i] = { INF, -1 };
 
        for (int x : v) {
            int cur_set = st[w[x]];
            if (!d[x]) {
                for (int i = x; i < M - 1; i += x) {
                    int j = nt[i + 1];
 
                    if (j < i + x) {
                        int oth_set = st[w[j]];
                        if (oth_set != cur_set) {
                            if (mn[cur_set].first > j - i)
                                mn[cur_set] = { j - i, oth_set };
                            if (mn[oth_set].first > j - i)
                                mn[oth_set] = { j - i, cur_set };
                        }
                    }
                }
            }
 
            // if (r_nt[x - 1] != -1) {
            //     int bef = r_nt[x - 1], oth_set = st[c[bef]];
            //     if (oth_set != cur_set) {
            //         mn[cur_set] = min(mn[cur_set], { x - bef, oth_set });
            //         mn[oth_set] = min(mn[oth_set], { x - bef, cur_set });
            //     }
            // }
        }
 
        for (int i = 0; i < cnt; i++) {
            if (q[i] < 0 && mn[i].second != -1 && connect(i, mn[i].second))  {
                ans += mn[i].first;
                remain--;
            }

        }

        for (int i = 0; i < cnt; i++) {
            st[i] = findSet(i);
        }
    }
 
    cout << ans;
 
 
    return 0;
}   

Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:76:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < v.size(); i++) {
                     ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 227 ms 168584 KB Output is correct
2 Correct 611 ms 169104 KB Output is correct
3 Correct 232 ms 170292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 568 ms 167104 KB Output is correct
2 Correct 989 ms 167580 KB Output is correct
3 Correct 221 ms 170488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 219 ms 169076 KB Output is correct
2 Correct 221 ms 163712 KB Output is correct
3 Correct 219 ms 170360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 954 ms 169648 KB Output is correct
2 Correct 1721 ms 169792 KB Output is correct
3 Correct 899 ms 169428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 363 ms 168408 KB Output is correct
2 Correct 1603 ms 168372 KB Output is correct
3 Correct 1335 ms 169076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1590 ms 169740 KB Output is correct
2 Correct 1782 ms 169724 KB Output is correct
3 Correct 767 ms 169844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 994 ms 168052 KB Output is correct
2 Correct 2060 ms 169460 KB Output is correct
3 Correct 816 ms 169560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 505 ms 178540 KB Output is correct
2 Correct 1645 ms 178536 KB Output is correct
3 Correct 629 ms 178540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 458 ms 178636 KB Output is correct
2 Correct 2420 ms 178468 KB Output is correct
3 Correct 488 ms 178548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 281 ms 177416 KB Output is correct
2 Correct 2660 ms 178224 KB Output is correct
3 Correct 866 ms 169716 KB Output is correct