Submission #370656

# Submission time Handle Problem Language Result Execution time Memory
370656 2021-02-24T12:13:45 Z Atill83 Sirni (COCI17_sirni) C++14
0 / 140
1216 ms 786436 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 3e5+5;
const int MAXM = (int) 1e7+5;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n;
int p[MAXN];
int buy[MAXM];

unordered_map<int, vector<pii>> edg;

int par[MAXN];
int sz[MAXN];
int find_set(int p){
    if(par[p] == p)
        return p;
    return par[p] = find_set(par[p]);
}

int merge(int a, int b){
    if(sz[b] > sz[a])
        swap(a, b);
    par[b] = a;
    sz[a] += sz[b];
}



int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);

    #ifdef Local
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
    #endif

    cin>>n;
    memset(buy, -1, sizeof(buy));
    for(int i = 0; i < n; i++){
        cin>>p[i];
        if(p[i] == 1){
            cout<<0<<endl;
            return 0;
        }
    }


    sort(p, p + n);
    n = (unique(p, p + n) - p);

    for(int i = 0; i < n; i++){
        buy[p[i]] = i;
        par[i] = i;
        sz[i] = 1;
    }
    for(int i = MAXM - 1; i > 0; i--){
        if(buy[i - 1] == -1)
            buy[i - 1] = buy[i];
    }


    for(int i = 0; i < n; i++){
        int fir = buy[p[i] + 1];
        if(fir != -1 && p[fir] < 2*p[i]){
            edg[p[fir] - p[i]].push_back({i, fir});
        }

        for(int j = 2*p[i]; j < MAXM; j += p[i]){
            if(buy[j] != -1 && p[buy[j]] < j + p[i]){
                edg[p[buy[j]] - j].push_back({i, buy[j]});
            }
        }
    }   
    ll ans = 0;
    for(int i = 0; i < MAXM; i++){
        if(edg.find(i) == edg.end())
            continue;
        for(auto u: edg[i]){
            int a = find_set(u.ff), b = find_set(u.ss);
            if(a != b){
                ans += i;
                merge(a, b);
            }
        }
    }
    //assert(sz[find_set(0)] == n);

    cout<<ans<<endl;


    #ifdef Local
        cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}

Compilation message

sirni.cpp: In function 'int merge(int, int)':
sirni.cpp:33:1: warning: no return statement in function returning non-void [-Wreturn-type]
   33 | }
      | ^
# Verdict Execution time Memory Grader output
1 Runtime error 720 ms 786436 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 807 ms 786436 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 716 ms 786436 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 946 ms 786436 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 756 ms 786436 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1216 ms 786436 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 956 ms 786436 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 863 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 884 ms 786436 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 767 ms 786436 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -