Submission #514890

#TimeUsernameProblemLanguageResultExecution timeMemory
514890ronnithSirni (COCI17_sirni)C++14
98 / 140
3885 ms786436 KiB
#include <bits/stdc++.h> #define index ind using namespace std; struct DSU { int cnt; int size; vector<int> e; DSU (int _) : size (_) { e = vector<int> (size, -1); } int root(int x) { return (e[x] < 0 ? x : (e[x] = root(e[x]))); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { if (same(x, y)) return; x = root(x), y = root(y); if (-e[x] > -e[y]) { swap(x, y); } e[y] += e[x]; e[x] = y; cnt --; } }; const int N = (int)1e5; const int Mx = (int)1e7; int n; int nxt[Mx + 1]; int index[Mx + 1]; vector<int> values; vector<pair<int, int>> edges[Mx + 1]; vector<tuple<int, int, int>> sr_edges; void solve() { cin >> n; for (int i = 0; i < n;i ++) { cin >> nxt[i]; } sort(nxt, nxt + n); values.push_back(nxt[0]); for (int i = 1; i < n;i ++) { if (nxt[i] != nxt[i - 1]) { values.push_back(nxt[i]); } } int ptr = 0; for (auto& e : values) { nxt[e] = 1; index[e] = ptr; ptr ++; } int crr = -1; for (int i = Mx; i > 0; i --) { int prev = crr; if (nxt[i] == 1) crr = i; nxt[i] = prev; } for (auto& e : values) { for (int i = e; i <= Mx; i += e) { if (i != e && index[i] != -1) { edges[0].push_back(make_pair(index[e], index[i])); } else if (nxt[i] != -1) { if (i + e <= nxt[i]) continue; edges[nxt[i] - i].push_back(make_pair(index[e], index[nxt[i]])); } } } for (int i = 0; i < Mx + 1; i ++) { for (auto& e : edges[i]) { sr_edges.push_back(make_tuple(e.first, e.second, i)); } } DSU d(n); int64_t ans = 0; for (auto& e : sr_edges) { int x, y, w; tie(x, y, w) = e; // cout << x + 1 << ' ' << y + 1 << ' ' << w << '\n'; if (!d.same(x, y)) { d.unite(x, y); ans += w; } } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); for (int i = 0; i < Mx + 1; i ++) { index[i] = -1; } solve(); return 0; }
#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...