#include <bits/stdc++.h>
using namespace std;
struct DSU{
int N;
vector<int> d;
void init(int n){
N = n;
d = vector<int>(N,-1);
}
int finden(int x){
if(d[x] < 0) return x;
return finden(d[x]);
}
bool unite(int a, int b){
a = finden(a), b = finden(b);
if(a == b) return false;
if(d[a] > d[b]){
swap(a,b);
} //absolute of d[a] is smaller than absolute of d[b], so there are less in a than in b, although we want to add b to a
d[a] += d[b]; d[b] = a;
return true;
}
}D;
struct edge{
int u,v,w;
bool operator<(const edge &b){
return w < b.w;
}
};
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N; cin >> N;
vector<int> P(N);
int maxx = 0;
for(int i = 0;i<N;++i){
cin >> P[i];
maxx = max(maxx,P[i]);
}
D.init(N);
P.resize(unique(P.begin(),P.end())-P.begin());
sort(P.begin(),P.end());
vector<edge> edges;
for(int i = 0;i<N;++i){
for(int j = P[i];j <= maxx; j += P[i]){
int it;
if(j == P[i]){
it = lower_bound(P.begin(),P.end(),j)-P.begin();
if(it == i && it+1 < P.size()){
int cur = P[it+1]-j;
edge e;
while(it+1 < P.size() && P[it+1]-j == cur){
++it;
e.u = i; e.v = it; e.w = P[it]-j;
edges.push_back(e);
}
continue;
}
}
else it = lower_bound(P.begin(),P.end(),j)-P.begin();
if(it == P.size()){
continue;
}
if(P[it]-j >= P[i]){
continue;
}
if(it < i){
continue;
}
edge e; e.u = i; e.v = it; e.w = P[it]-j;
edges.push_back(e);
int cur = P[it]-j;
while(it+1 < P.size() && P[it+1]-j == cur){
++it;
e.u = i; e.v = it; e.w = P[it]-j;
edges.push_back(e);
}
}
}
sort(edges.begin(),edges.end());
int ans = 0, cnt = N-1;
for(int i = 0;i<edges.size();++i){
if(D.unite(edges[i].u,edges[i].v)){
ans += edges[i].w;
--cnt;
}
if(cnt == 0) break;
}
cout << ans << "\n";
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:49:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | if(it == i && it+1 < P.size()){
| ~~~~~^~~~~~~~~~
sirni.cpp:52:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | while(it+1 < P.size() && P[it+1]-j == cur){
| ~~~~~^~~~~~~~~~
sirni.cpp:61:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | if(it == P.size()){
| ~~~^~~~~~~~~~~
sirni.cpp:73:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | while(it+1 < P.size() && P[it+1]-j == cur){
| ~~~~~^~~~~~~~~~
sirni.cpp:83:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int i = 0;i<edges.size();++i){
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Incorrect |
59 ms |
3464 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
142 ms |
25980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
3792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
339 ms |
50568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
49 ms |
6992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
170 ms |
25748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
178 ms |
25988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
3696 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |