#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
using i64 = long long;
#define rep(i,l,r) for (int i = (l); i <= (r); i++)
const int MaxN = 1e5;
const int MaxA = 1e6 + 1;
int N, A[MaxN];
struct DSU {
int e[MaxN];
void initSz(int n) {
memset(e, -1, n * sizeof(int));
}
int fr(int x) { return e[x] < 0 ? x : e[x] = fr(e[x]); }
bool unite(int x, int y) {
x = fr(x), y = fr(y);
if (x==y) return false;
if (e[x] > e[y]) swap(x,y);
e[x] += e[y];
e[y] = x;
return true;
}
};
DSU dsu;
void sub1() {
sort(A, A + N);
N = unique(A, A + N) - A;
vector<array<int,3>> edges;
rep(i, 0, N-1) {
rep(j, i+1, N-1) {
edges.push_back({A[j] % A[i], i, j});
}
}
sort(all(edges));
dsu.initSz(N);
long long res = 0;
for (int i = 0; i < edges.size(); i++) {
int w = edges[i][0];
int u = edges[i][1];
int v = edges[i][2];
if (dsu.unite(u, v)) res += w;
}
cout << res;
}
void sub2() {
sort(A, A + N);
N = unique(A, A + N) - A;
vector<array<int,3>> edges;
for (int i = 0; i < N; i++) {
for (int mul = A[i]; mul <= MaxA; mul += A[i]) {
int j = upper_bound(A, A + N, mul) - A;
if (j < N) {
edges.push_back({A[j] % A[i], i, j});
}
}
}
sort(all(edges));
dsu.initSz(N);
long long res = 0;
for (int i = 0; i < edges.size(); i++) {
int w = edges[i][0];
int u = edges[i][1];
int v = edges[i][2];
if (dsu.unite(u, v)) res += w;
}
cout << res;
}
signed main() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
if (N <= 2e3) {
sub1();
}
else {
sub2();
}
return 0;
}
Compilation message
sirni.cpp: In function 'void sub1()':
sirni.cpp:45:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int i = 0; i < edges.size(); i++) {
| ~~^~~~~~~~~~~~~~
sirni.cpp: In function 'void sub2()':
sirni.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 0; i < edges.size(); i++) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
8140 KB |
Output is correct |
2 |
Correct |
47 ms |
6860 KB |
Output is correct |
3 |
Correct |
51 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
6860 KB |
Output is correct |
2 |
Correct |
31 ms |
4044 KB |
Output is correct |
3 |
Correct |
50 ms |
6860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
7116 KB |
Output is correct |
2 |
Correct |
39 ms |
6860 KB |
Output is correct |
3 |
Correct |
50 ms |
8396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
268 ms |
26500 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
39 ms |
3536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
569 ms |
50476 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
107 ms |
7364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
51 ms |
2704 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
60 ms |
5044 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |