이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |