제출 #636448

#제출 시각아이디문제언어결과실행 시간메모리
636448dozerSirni (COCI17_sirni)C++14
140 / 140
4459 ms244768 KiB
#include <bits/stdc++.h> using namespace std; #define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout) #define fastio() cin.tie(0), ios_base::sync_with_stdio(0) #define sp " " #define endl "\n" #define pb push_back #define pii pair<int , int> #define st first #define nd second #define N 10000005 int root[N], sz[N], arr[N]; vector<int> ind[N]; set<int> roots; int find(int node) { if (root[node] == node) return node; return root[node] = find(root[node]); } void uni(int a, int b) { a = find(a), b = find(b); if (a == b) return; if (sz[a] > sz[b]) swap(a, b); roots.erase(a); root[a] = b; sz[b] += sz[a]; } int32_t main() { fastio(); int n; cin>>n; int maks = 0; vector<int> v; for (int i = 1; i <= n; i++) { cin>>arr[i]; if (ind[arr[i]].size() == 0) v.pb(arr[i]); ind[arr[i]].pb(i); root[i] = i; sz[i] = 1; roots.insert(i); maks = max(maks, arr[i]); } for (int i = 1; i <= n; i++) uni(i, ind[arr[i]].front()); int rem = 0; int ans = 0; while(roots.size() > 1) { for (auto curr : v) { int i = ind[curr].front(); for (int j = rem; j <= maks; j += curr) { if (ind[j].size() == 0) continue; int k = ind[j].front(); if (find(i) != find(k)) uni(i, k), ans += rem; } } rem++; } cout<<ans<<endl; cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n"; }
#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...