Submission #624736

#TimeUsernameProblemLanguageResultExecution timeMemory
624736BhavayGoyalSirni (COCI17_sirni)C++14
84 / 140
2508 ms786432 KiB
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define int long long #define ld long double #define ar array #define vi vector<int> #define vii vector<vector<int>> #define pii pair<int, int> #define pb push_back #define all(x) x.begin(), x.end() #define f first #define s second #define endl "\n" const int MOD = 1e9+7; const int inf = 1e9; const int linf = 1e18; const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1}; const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1}; // -------------------------------------------------- Main Code -------------------------------------------------- const int N = 1e5+5; int par[N], sz[N]; void make(int n) {for (int i = 0; i < n; i++) par[i] = i, sz[i] = 1;} int find(int v) {if (par[v] == v) return v; return par[v] = find(par[v]);} bool Union(int a, int b) { a = find(a); b = find(b); if (a == b) return false; if (sz[a] < sz[b]) swap(a, b); par[b] = a; sz[a] += sz[b]; return true; } const int M = 1e7+5; vector<array<int, 2>> edges[M]; void sol() { int n; cin >> n; vi arr(n); for (int i = 0; i < n; i++) cin >> arr[i]; sort (all(arr)); arr.erase(unique(all(arr)), arr.end()); n = arr.size(); vi next(M, -1); for (int i = 0; i < n; i++) next[arr[i]] = i; for (int i = M-2; i >= 0; i--) if (next[i] == -1) next[i] = next[i+1]; for (int i = 0; i < n; i++) { for (int j = arr[i]; j < M; j += arr[i]) { int idx = next[j+(j==arr[i])]; if (idx == -1) continue; int dist = min(arr[idx]%arr[i], arr[i]%arr[idx]); edges[dist].pb({i, idx}); } } int ans = 0; for (int i = 0; i < M; i++) { for (auto j : edges[i]) { if (Union(j[0], j[1])) ans += i; } } cout << ans << endl; } signed main () { ios_base::sync_with_stdio(false); cin.tie(NULL); make(N); int t = 1; // cin >> t; while (t--) { sol(); } 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...