Submission #46361

# Submission time Handle Problem Language Result Execution time Memory
46361 2018-04-19T18:28:25 Z evpipis Sirni (COCI17_sirni) C++11
98 / 140
5000 ms 330300 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(), c < '0' || c > '9'){}
    res = c-'0';
    while (c = getchar(), 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].reserve(cnt_sz[i]);
    }

    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;
        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 276 ms 278392 KB Output is correct
2 Correct 441 ms 279140 KB Output is correct
3 Correct 280 ms 279140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 187 ms 279140 KB Output is correct
2 Correct 1098 ms 279140 KB Output is correct
3 Correct 288 ms 279140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 278 ms 279140 KB Output is correct
2 Correct 287 ms 279140 KB Output is correct
3 Correct 286 ms 279156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 302 ms 279156 KB Output is correct
2 Correct 535 ms 290456 KB Output is correct
3 Correct 317 ms 290456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 232 ms 290456 KB Output is correct
2 Correct 469 ms 290456 KB Output is correct
3 Correct 268 ms 290456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 394 ms 290456 KB Output is correct
2 Correct 529 ms 303348 KB Output is correct
3 Correct 308 ms 303348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 212 ms 303348 KB Output is correct
2 Correct 622 ms 303348 KB Output is correct
3 Correct 293 ms 303348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 996 ms 330300 KB Output is correct
2 Execution timed out 5023 ms 330300 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 969 ms 330300 KB Output is correct
2 Execution timed out 5095 ms 330300 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 414 ms 330300 KB Output is correct
2 Execution timed out 5111 ms 330300 KB Time limit exceeded
3 Halted 0 ms 0 KB -