#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int MAXN = 1e7+10;
vector<pair<int,int>> edges[MAXN];
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;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N; cin >> N;
vector<int> P(N);
for(int i = 0;i<N;++i){
cin >> P[i];
}
P.resize(unique(P.begin(),P.end())-P.begin());
N = P.size();
D.init(N);
sort(P.begin(),P.end());
for(int i = 0;i<N;++i){
for(int j = P[i];j <= P.back(); j += P[i]){
auto it = lower_bound(P.begin()+i+1,P.end(),j);
if(it == P.end()) continue;
int it2 = it-P.begin();
if(P[it2]-j >= P[i]){
continue;
}
edges[P[it2]-j].push_back({it2,i});
}
}
long long ans = 0, cnt = N-1;
for(int i = 0;i<MAXN;++i){
for(auto p : edges[i]){
if(D.unite(p.f,p.s)){
ans += i;
}
}
}
cout << ans << "\n";
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:52:24: warning: unused variable 'cnt' [-Wunused-variable]
52 | long long ans = 0, cnt = N-1;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
235408 KB |
Output is correct |
2 |
Correct |
152 ms |
238416 KB |
Output is correct |
3 |
Correct |
128 ms |
235348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
235348 KB |
Output is correct |
2 |
Correct |
671 ms |
236112 KB |
Output is correct |
3 |
Correct |
110 ms |
235344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
235196 KB |
Output is correct |
2 |
Correct |
112 ms |
235092 KB |
Output is correct |
3 |
Correct |
122 ms |
235340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
201 ms |
246132 KB |
Output is correct |
2 |
Correct |
515 ms |
283444 KB |
Output is correct |
3 |
Correct |
267 ms |
256068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
236872 KB |
Output is correct |
2 |
Correct |
1171 ms |
503548 KB |
Output is correct |
3 |
Correct |
216 ms |
247840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
260956 KB |
Output is correct |
2 |
Correct |
742 ms |
314152 KB |
Output is correct |
3 |
Correct |
254 ms |
250548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
239632 KB |
Output is correct |
2 |
Correct |
570 ms |
304148 KB |
Output is correct |
3 |
Correct |
260 ms |
253764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
215 ms |
248920 KB |
Output is correct |
2 |
Correct |
2196 ms |
533248 KB |
Output is correct |
3 |
Correct |
254 ms |
251524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
248440 KB |
Output is correct |
2 |
Correct |
3167 ms |
646972 KB |
Output is correct |
3 |
Correct |
339 ms |
258128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
237648 KB |
Output is correct |
2 |
Correct |
3089 ms |
681768 KB |
Output is correct |
3 |
Correct |
275 ms |
256196 KB |
Output is correct |