Submission #715438

#TimeUsernameProblemLanguageResultExecution timeMemory
715438TheSahibSirni (COCI17_sirni)C++17
0 / 140
618 ms58604 KiB
#include <bits/stdc++.h> #define ll long long #define pii pair<int, int> using namespace std; const int MAX = 1e5 + 5; int maxVal = 0; set<int> s; struct edge{ int u, v, weight; bool operator<(edge e){ return weight < e.weight; } }; int par[10000007]; int findPar(int u){ if(par[u] < 0) return u; return par[u] = findPar(par[u]); } bool setUnion(int u, int v){ u = findPar(u); v = findPar(v); if(u == v) return false; if(-par[u] < -par[v]) swap(u, v); par[u] += par[v]; par[v] = u; return true; } int main(){ int n; cin >> n; for (int i = 0; i < n; i++) { int a; cin >> a; s.insert(a); } maxVal = *(prev(s.end())); vector<edge> edges; for(int a:s){ for(int j = a; j <= maxVal; j += a){ int b = *s.lower_bound(j); if((b / a) == (j / a)){ edges.push_back({a, b, b % a}); } } } fill(par, par + maxVal + 1, -1); sort(edges.begin(), edges.end()); ll cost = 0; for(edge& e:edges){ if(setUnion(e.u, e.v)){ cost += e.weight; } } cout << cost << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...