Submission #139924

# Submission time Handle Problem Language Result Execution time Memory
139924 2019-08-01T16:36:49 Z Minnakhmetov Sirni (COCI17_sirni) C++14
126 / 140
5000 ms 178316 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];
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;
 
    mt19937 rnd(228);
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x; 
 
        // x = rnd() % 20 + 1;
 
        // cout << x << "\n";
 
        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 < M; i++) {
    //     if (!d[i] && b[i])
    //         cout << i << "\n";
    // }
    // return 0;
 
    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];
    }
 
    // for (int i = 0; i < 20; i++) {
    //     cout << nt[i] << " ";
    // }
    // cout << "\n";
    // for (int i = 0; i < 20; i++) {
    //     cout << w[i] << " ";
    // }
    // cout << "\n";
 
    // for (int x : v)
    //     cout << x << "\n";
    // return 0;
 
    ll ans = 0;
 
    while (remain > 1) {
        for (int i = 0; i < cnt; i++)
            mn[i] = { INF, -1 };
 
        for (int k = 0; k < v.size(); k++) {
            int x = v[k], cur_set = findSet(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 = findSet(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 = findSet(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++) {
            // cout << mn[i].first << " " << mn[i].second << "\n";
            if (q[i] < 0 && mn[i].second != -1 && connect(i, mn[i].second))  {
                ans += mn[i].first;
                remain--;
            }
        }
    
    }
 
    cout << ans;
 
 
    return 0;
}   

Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:89:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < v.size(); i++) {
                     ~~^~~~~~~~~~
sirni.cpp:127:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int k = 0; k < v.size(); k++) {
                         ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 220 ms 168464 KB Output is correct
2 Correct 546 ms 168952 KB Output is correct
3 Correct 224 ms 170476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 552 ms 167232 KB Output is correct
2 Correct 943 ms 167572 KB Output is correct
3 Correct 230 ms 170488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 220 ms 169072 KB Output is correct
2 Correct 218 ms 163704 KB Output is correct
3 Correct 224 ms 170496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1106 ms 169300 KB Output is correct
2 Correct 1922 ms 169260 KB Output is correct
3 Correct 811 ms 169204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 378 ms 168304 KB Output is correct
2 Correct 1686 ms 168312 KB Output is correct
3 Correct 1220 ms 168720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2057 ms 169540 KB Output is correct
2 Correct 1821 ms 169472 KB Output is correct
3 Correct 832 ms 169464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 998 ms 167888 KB Output is correct
2 Correct 2240 ms 169096 KB Output is correct
3 Correct 810 ms 169280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1121 ms 178140 KB Output is correct
2 Correct 2349 ms 178272 KB Output is correct
3 Correct 1275 ms 178316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 813 ms 178136 KB Output is correct
2 Correct 4475 ms 178020 KB Output is correct
3 Correct 451 ms 178140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 386 ms 177272 KB Output is correct
2 Execution timed out 5054 ms 177776 KB Time limit exceeded
3 Halted 0 ms 0 KB -