#include <bits/stdc++.h>
#define index ind
using namespace std;
struct DSU {
int cnt;
int size;
vector<int> e;
DSU (int _) : size (_) {
e = vector<int> (size, -1);
}
int root(int x) {
return (e[x] < 0 ? x : (e[x] = root(e[x])));
}
bool same(int x, int y) {
return root(x) == root(y);
}
void unite(int x, int y) {
if (same(x, y)) return;
x = root(x), y = root(y);
if (-e[x] > -e[y]) {
swap(x, y);
}
e[y] += e[x];
e[x] = y;
cnt --;
}
};
const int N = (int)1e5;
const int Mx = (int)1e7;
int n;
int a[N];
int nxt[Mx + 1];
vector<pair<int, int>> edges[Mx + 1];
vector<tuple<int, int, int>> sr_edges;
void solve() {
cin >> n;
for (int i = 0; i < n;i ++) {
cin >> a[i];
}
sort(a, a + n);
int ptr = 1;
for (int i = 1; i < n;i ++) {
if (a[i] != a[i - 1]) {
a[ptr] = a[i];
ptr ++;
}
}
n = ptr;
for (int i = 0; i < n; i ++) {
nxt[a[i]] = -(i + 1);
}
for (int i = a[n - 1] - 1; i >= 0; i --) {
if (nxt[i] == 0) {
nxt[i] = nxt[i + 1];
if (nxt[i] < 0) nxt[i] *= -1;
}
}
for (int i = 0; i < n - 1; i ++) {
for (int j = a[i]; j <= a[n - 1]; j += a[i]) {
int nt;
if (j == a[i]) {
nt = (nxt[a[i] + 1] < 0 ? -nxt[a[i] + 1] : nxt[a[i] + 1]);
} else {
nt = (nxt[j] < 0 ? -nxt[j] : nxt[j]);
}
if (a[nt - 1] >= j + a[i]) continue;
edges[a[nt - 1] - j].push_back(make_pair(i + 1, nt));
}
}
for (int i = 0; i < a[n - 1]; i ++) {
for (auto& e : edges[i]) {
sr_edges.push_back(make_tuple(e.first, e.second, i));
}
}
DSU d(n);
int64_t ans = 0;
for (auto& e : sr_edges) {
int x, y, w; tie(x, y, w) = e;
x --, y --;
if (!d.same(x, y)) {
d.unite(x, y);
ans += w;
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
183 ms |
274532 KB |
Output is correct |
2 |
Correct |
248 ms |
279860 KB |
Output is correct |
3 |
Correct |
187 ms |
274664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
235400 KB |
Output is correct |
2 |
Correct |
529 ms |
275788 KB |
Output is correct |
3 |
Correct |
196 ms |
274776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
185 ms |
274480 KB |
Output is correct |
2 |
Correct |
208 ms |
274280 KB |
Output is correct |
3 |
Correct |
183 ms |
274572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
205 ms |
261972 KB |
Output is correct |
2 |
Correct |
364 ms |
327248 KB |
Output is correct |
3 |
Correct |
258 ms |
280064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
243924 KB |
Output is correct |
2 |
Correct |
297 ms |
311120 KB |
Output is correct |
3 |
Correct |
200 ms |
261252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
259 ms |
311240 KB |
Output is correct |
2 |
Correct |
451 ms |
390824 KB |
Output is correct |
3 |
Correct |
200 ms |
278804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
245876 KB |
Output is correct |
2 |
Correct |
368 ms |
386420 KB |
Output is correct |
3 |
Correct |
200 ms |
278180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
336 ms |
312160 KB |
Output is correct |
2 |
Runtime error |
1646 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
328 ms |
311864 KB |
Output is correct |
2 |
Runtime error |
3206 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
217 ms |
279968 KB |
Output is correct |
2 |
Runtime error |
3609 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |