이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> e;
DSU(int N) {e = vector<int>(N, -1);}
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool same_set(int a, int b) { return get(a) == get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int y) {
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y]; e[y] = x; return true;
}
};
const int N=1e7+10;
vector<int> great(N,1e9);
vector<pair<int,int>> ed[N/2];
vector<int> v;
DSU d(1e5+10);
int main () {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
v.resize(n);
{
for(int i = 0;i<n;i++) {
cin >> v[i];
}
sort(v.begin(), v.end() );
v.erase( unique( v.begin(), v.end() ), v.end() );
}
n=v.size();
for(int i = 0;i<n;i++)great[v[i]]=i;
for(int j =v[n-1];j>=0;j--) {
great[j]=min(great[j],great[j+1]);
}
for(int i=0;i<n;i++) {
for(int j=v[i];j<=v[n-1];j+=v[i]) {
if(j!=v[i]) {
if(great[j]<n) {
ed[v[great[j]]-j].push_back({i,great[j]});
}
}
else if(great[j+1]<n) {
ed[v[great[j+1]]-j].push_back({i,great[j+1]});
}
}
}
long long sum=0;
for(int i = 0;i<=v[n-1];i++) {
for(auto j:ed[i]) {
sum+=(int)d.unite(j.second,j.first)*i;
}
}
cout << sum << "\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... |