Submission #1154357

#TimeUsernameProblemLanguageResultExecution timeMemory
1154357cpismylifeOwOSirni (COCI17_sirni)C++20
140 / 140
1445 ms613720 KiB
#include <bits/stdc++.h> using namespace std; const int inf = 1e7 + 1; const long long mod = 1e9 + 7; const int MaxN = 1e5 + 5; const int MaxA = 1e7 + 5; int n; int arr[MaxN]; void Inp() { cin >> n; for (int x = 1; x <= n; x++) { cin >> arr[x]; } sort(arr + 1, arr + n + 1); } int parents[MaxN]; int sz[MaxN]; pair<int, int> cur[MaxN]; int FindSet(int p) { if (parents[p] == p) { return p; } parents[p] = FindSet(parents[p]); return parents[p]; } bool UnionSet(int a, int b) { a = FindSet(a); b = FindSet(b); if (a == b) { return false; } if (sz[a] < sz[b]) { swap(a, b); } parents[b] = a; sz[a] += sz[b]; return true; } int pre[MaxA]; int dis[MaxA]; pair<int, int> now[MaxN]; vector<pair<int, int>> cntsrt[MaxA]; bool mark[MaxN]; void Exc() { int mxa = 0; for (int x = 1; x <= n; x++) { parents[x] = x; sz[x] = 1; mxa = max(mxa, arr[x]); } int cnt = 0; for (int x = 1; x <= n; x++) { int y = x; while (y <= n && arr[x] == arr[y]) { UnionSet(x, y); mark[y] = false; y++; } y--; mark[y] = true; pre[arr[x]] = y; cnt++; x = y; } for (int x = mxa; x > 0; x--) { dis[x] = 0; if (!pre[x]) { pre[x] = pre[x + 1]; dis[x] = dis[x + 1] + 1; } } for (int x = 1; x <= n; x++) { if (!mark[x]) { continue; } if (arr[x] < mxa && dis[arr[x] + 1] + 1 < arr[x]) { int p = pre[arr[x] + 1], k = min(arr[x] % arr[p], arr[p] % arr[x]); cntsrt[k].push_back(make_pair(x, p)); } for (int y = arr[x] + arr[x]; y <= mxa; y += arr[x]) { int q = dis[y], p = pre[y]; if (q < arr[x]) { int k = min(arr[x] % arr[p], arr[p] % arr[x]); cntsrt[k].push_back(make_pair(x, p)); } else { y += ((q / arr[x]) - 1) * arr[x]; } } } long long res = 0; for (int x = 0; x <= mxa && cnt > 1; x++) { for (pair<int, int> y : cntsrt[x]) { int u = y.first, v = y.second; if (UnionSet(u, v)) { res += min(arr[u] % arr[v], arr[v] % arr[u]); cnt--; } } } cout << res; } int main() { //freopen("A.INP", "r", stdin); //freopen("A.OUT", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(nullptr); int test = 1; //cin >> test; for (int x = 1; x <= test; x++) { Inp(); Exc(); } 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...