이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <bits/stdc++.h>
#include <functional>
#include <numeric>
using namespace std;
int main(){
int N;
cin >> N;
set<int> s;
int a;
for(int i = 0; i < N; ++i){
cin >> a;
s.insert(a);
}
N = s.size();
vector<int> v;
for(auto x: s)v.emplace_back(x);
int m = 1e7 + 2;
long long ans = 0;
vector<array<int,3>> eg;
for(int i = 0; i < N; ++i){
for(int j = v[i]; j <= v.back(); j += v[i]){
int k = lower_bound(v.begin() + i + 1, v.end(), j) -v.begin();
if(k >= N)continue;
eg.push_back({v[k] - j, k, i});
}
}
vector<int> dad(N), sz(N, 1);
iota(dad.begin(), dad.end(), 0);
function<int(int)> find=[&](int a){
if(dad[a] != a)dad[a] = find(dad[a]);
return dad[a];
};
auto uni = [&](int a, int b) -> bool{
a = find(a);
b = find(b);
if(a == b)return 0;
if(sz[a] < sz[b])swap(a,b);
dad[b] = a;
sz[a] += sz[b];
return 1;
};
sort(eg.begin(), eg.end());
for(auto [w, i, j]: eg){
if(uni(i, j))ans += w;
}
cout << ans << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sirni.cpp: In function 'int main()':
sirni.cpp:22:9: warning: unused variable 'm' [-Wunused-variable]
22 | int m = 1e7 + 2;
| ^
# | 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... |