Submission #1194225

#TimeUsernameProblemLanguageResultExecution timeMemory
1194225SnowRaven52Sirni (COCI17_sirni)C++20
0 / 140
157 ms274680 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5; const int mx = 1e7; int n, a[maxn], nxt[mx + 1]; vector<pair<int, int>> edges[mx + 1]; class DisjointSets { private: vector<int> parents; vector<int> sizes; public: DisjointSets(int size) : parents(size), sizes(size, 1) { for (int i = 0; i < size; i++) { parents[i] = i; } } int find(int x) { return parents[x] == x ? x : (parents[x] = find(parents[x])); } bool unite(int x, int y) { int x_root = find(x); int y_root = find(y); if (x_root == y_root) { return false; } if (sizes[x_root] < sizes[y_root]) { swap(x_root, y_root); } sizes[x_root] += sizes[y_root]; parents[y_root] = x_root; return true; } bool connected(int x, int y) { return find(x) == find(y); } }; int main() { // freopen("main.in", "r", stdin); // freopen(".out", "w", stdout); cin >> n; for(int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int p = 1; for(int i = 1; i < n; i++) if(a[i] != a[i - 1]) a[p] = a[i], p++; n = p; for(int i = 0; i < n; i++) nxt[a[i]] = -(i + 1); for(int i = a[n-1] - 1; i >= 0; i--) { if(nxt[i] == 0){ nxt[i] = nxt[i + 1]; if (nxt[i] < 0) nxt[i] = -(nxt[i]); } } for(int i = 0; i < n - 1; i++){ for(int j = a[i]; j <= a[n - 1]; j+=a[i]) { int nt; if(j == a[i]) nt = (nxt[a[i]] < 0 ? -nxt[a[i] + 1] : nxt[a[i]+1]); else nt = (nxt[j] < 0 ? -nxt[j] : nxt[j]); if(a[nt - 1] >= a[i] + j) continue; edges[a[nt - 1] - j].push_back({i+1, nt}); } } long long ans = 0; DisjointSets dsu(n); for(int i = 0; i < a[n-1]; i++){ for(auto &edge: edges[i]){ if(!dsu.connected(edge.first - 1, edge.second - 1)){ dsu.unite(edge.first - 1, edge.second - 1); ans += i; } } } cout << ans << endl; }
#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...