Submission #551566

#TimeUsernameProblemLanguageResultExecution timeMemory
551566chenwzSirni (COCI17_sirni)C++14
140 / 140
4894 ms395992 KiB
#include <bits/stdc++.h> using namespace std; int const maxn = 1e5 + 4; int n = 0; int p[maxn], Mx = 0; int fa[maxn]; //, siz[maxn]; int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } inline void merge(int x, int y) { x = find(x), y = find(y), fa[y] = x; } struct Edge { int x, y, w; bool operator<(const Edge &e) const { return w < e.w; } }; int main() { ios::sync_with_stdio(false), cin.tie(0); vector<Edge> es; cin >> n; for (int i = 1; i <= n; ++i) cin >> p[i]; sort(p + 1, p + n + 1); n = unique(p + 1, p + n + 1) - p - 1; Mx = p[n]; for (int i = 1; i <= n + 1; ++i) fa[i] = i; long long ans = 0; for (int i = 1; i <= n; ++i) { int x = p[i]; for (int y = x; y <= Mx; y += x) { int k = lower_bound(p + i + 1, p + n + 1, y) - p; if (k <= n && p[k] / x == y / x) es.push_back(Edge{i, k, p[k] % p[i]}); } } sort(es.begin(), es.end()); for (auto e : es) { int u = find(e.y), v = find(e.x); if (u != v) ans += e.w, fa[u] = v; // merge(v, u); } cout << ans << endl; 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...