Submission #46359

# Submission time Handle Problem Language Result Execution time Memory
46359 2018-04-19T18:14:13 Z evpipis Sirni (COCI17_sirni) C++11
56 / 140
5000 ms 786432 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;
int cnt[100*len], nex[100*len][2], p[len], r[len];
int u[30 * len], v[30 * 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;
    }

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

        nex[i][1] = nex[i+1][1];
        if (cnt[i+1]) nex[i][1] = 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]) {
            for (int j = i; j <= m; j+=i){
                if (j == i && nex[j][1] && nex[j][1] < j+i){
                    cnt_sz[nex[j][1]-j]++;
                }
                else if (j != i && nex[j][0] && nex[j][0] < j+i){
                    cnt_sz[nex[j][0]-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])
            for (int j = i; j <= m; j+=i){
                if (j == i && nex[j][1] && nex[j][1] < j+i) {
                    u[edcnt] = cnt[i];
                    v[edcnt] = cnt[nex[j][1]];
                    vec[nex[j][1]-j].pb(edcnt);
                    edcnt++;
                    //vec[nex[j][1]-j].pb(mp(cnt[i], cnt[nex[j][1]]));
                }
                else if (j != i && nex[j][0] && nex[j][0] < j+i){
                    u[edcnt] = cnt[i];
                    v[edcnt] = cnt[nex[j][0]];
                    vec[nex[j][0]-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 354 ms 317812 KB Output is correct
2 Correct 469 ms 318072 KB Output is correct
3 Correct 306 ms 318072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 190 ms 318072 KB Output is correct
2 Correct 1477 ms 318072 KB Output is correct
3 Correct 312 ms 318200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 327 ms 318200 KB Output is correct
2 Correct 309 ms 318200 KB Output is correct
3 Correct 319 ms 318256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 358 ms 318256 KB Output is correct
2 Runtime error 983 ms 568324 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 230 ms 568324 KB Output is correct
2 Correct 502 ms 568324 KB Output is correct
3 Correct 258 ms 568324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 586 ms 568324 KB Output is correct
2 Runtime error 961 ms 568380 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 204 ms 568380 KB Output is correct
2 Runtime error 881 ms 569668 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 834 ms 652524 KB Output is correct
2 Runtime error 4243 ms 784640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 833 ms 784640 KB Output is correct
2 Execution timed out 5100 ms 786432 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 402 ms 786432 KB Output is correct
2 Execution timed out 5138 ms 786432 KB Time limit exceeded
3 Halted 0 ms 0 KB -