Submission #46363

# Submission time Handle Problem Language Result Execution time Memory
46363 2018-04-19T18:38:44 Z evpipis Sirni (COCI17_sirni) C++11
98 / 140
5000 ms 95296 KB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef pair<int, int> ii;
typedef long long ll;

const int len = 1e5+2, inf = 1e9;
int cnt[100*len], nex[100*len], p[len], r[len];
int u[100*len], v[100*len], edcnt = 0;
int cnt_sz[100*len];
vector<int> *vec[100*len];

int fin(int i){
    if (p[i] == i) return i;
    return p[i] = fin(p[i]);
}

void join(int i, int j){
    if (r[i] > r[j])
        p[j] = i;
    else{
        p[i] = j;
        if (r[i] == r[j]) r[j]++;
    }
}

void read(int &res){
    char c;
    while (c = getchar_unlocked(), c < '0' || c > '9'){}
    res = c-'0';
    while (c = getchar_unlocked(), c >= '0' && c <= '9')
        res = 10*res+c-'0';
}

int main(){
    int n, m = 0, k = 0;
    read(n);
    for (int i = 0; i < n; i++){
        int temp;
        read(temp);

        m = max(m, temp);
        if (!cnt[temp]) cnt[temp] = ++k;
    }

    nex[m+1] = inf;
    for (int i = m; i >= 1; i--){
        nex[i] = nex[i+1];
        if (cnt[i+1]) nex[i] = i+1;
    }

    for (int i = 1; i <= k; i++){
        r[i] = 0;
        p[i] = i;
    }

    for (int i = 1; i <= m; i++) {
        if (cnt[i]) {
            if (nex[i] < 2*i){
                cnt_sz[nex[i]-i]++;
            }
            for (int j = 2*i; j <= m; j+=i){
                if (cnt[j]){
                    cnt_sz[0]++;
                }
                else if (nex[j] < j+i){
                    cnt_sz[nex[j]-j]++;
                }
            }
        }
    }

    for (int i = 0; i <= m; i++){
        if (cnt_sz[i]) {
            vec[i] = new vector<int>();
            vec[i]->reserve(cnt_sz[i]);
        }
    }
    vec[0] = new vector<int>();

    for (int i = 1; i <= m; i++)
        if (cnt[i]){
            if (nex[i] < 2*i){
                u[edcnt] = cnt[i];
                v[edcnt] = cnt[nex[i]];
                vec[nex[i]-i]->pb(edcnt);
                edcnt++;
                //vec[nex[j][1]-j].pb(mp(cnt[i], cnt[nex[j][1]]));
            }
            for (int j = i; j <= m; j+=i){
                if (cnt[j]){
                    u[edcnt] = cnt[i];
                    v[edcnt] = cnt[j];
                    vec[0]->pb(edcnt);
                    edcnt++;
                }
                else if (nex[j] < j+i){
                    u[edcnt] = cnt[i];
                    v[edcnt] = cnt[nex[j]];
                    vec[nex[j]-j]->pb(edcnt);
                    edcnt++;
                    //vec[nex[j][0]-j].pb(mp(cnt[i], cnt[nex[j][0]]));
                }
            }
        }

    ll ans = 0;
    for (int i = 0; i <= m; i++){
        if (k == 1) break;
        if (vec[i] == NULL) continue;
        for (auto it : *vec[i]){
            if (k == 1) break;
            int x = fin(u[it]), y = fin(v[it]);
            if (x != y)
                join(x, y), ans += i, k--;
        }
    }

    printf("%lld\n", ans);
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 91 ms 44284 KB Output is correct
2 Correct 268 ms 45128 KB Output is correct
3 Correct 101 ms 45128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 45128 KB Output is correct
2 Correct 919 ms 45128 KB Output is correct
3 Correct 98 ms 45128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 45128 KB Output is correct
2 Correct 95 ms 45128 KB Output is correct
3 Correct 91 ms 45128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 45128 KB Output is correct
2 Correct 356 ms 55764 KB Output is correct
3 Correct 128 ms 55764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 55764 KB Output is correct
2 Correct 387 ms 55764 KB Output is correct
3 Correct 65 ms 55764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 255 ms 55764 KB Output is correct
2 Correct 358 ms 68580 KB Output is correct
3 Correct 126 ms 68580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 68580 KB Output is correct
2 Correct 431 ms 68580 KB Output is correct
3 Correct 97 ms 68580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 749 ms 95296 KB Output is correct
2 Execution timed out 5031 ms 95296 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 713 ms 95296 KB Output is correct
2 Execution timed out 5033 ms 95296 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 221 ms 95296 KB Output is correct
2 Execution timed out 5018 ms 95296 KB Time limit exceeded
3 Halted 0 ms 0 KB -