Submission #393452

#TimeUsernameProblemLanguageResultExecution timeMemory
393452ngpin04Sirni (COCI17_sirni)C++14
140 / 140
4536 ms613000 KiB
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair using namespace std; const int N = 1e7 + 5; vector <pair <int, int>> edge[N]; int a[N]; int p[N]; int par[N]; int n; bool mini(int &a, int b) { if (a > b) { a = b; return true; } return false; } int getpar(int u) { return (par[u] < 0) ? u : (par[u] = getpar(par[u])); } bool join(int u, int v) { u = getpar(u); v = getpar(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() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; int mx = 0; for (int i = 1; i <= n; i++) { int x; cin >> x; p[x] = x; mx = max(mx, x); } for (int i = mx - 1; i >= 1; i--) if (!p[i]) p[i] = p[i + 1]; for (int i = 1; i <= mx; i++) if (p[i] == i) { if (i < mx && p[i + 1] < i + i) { edge[p[i + 1] - i].push_back(mp(i, p[i + 1])); } for (int j = 2 * i; j <= mx; j += i) { // cout << p[j] << " " << j << "\n"; if (p[j] < j + i) edge[p[j] - j].push_back(mp(i, p[j])); } } for (int i = 1; i <= mx; i++) par[i] = -1; long long ans = 0; for (int i = 0; i <= mx; i++) for (pair <int, int> e : edge[i]) ans += i * join(e.fi, e.se); cout << ans; 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...