#include <bits/stdc++.h>
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];
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));
}
}
int64_t ans = 0;
DSU d(n);
for (int i = 0; i < a[n - 1]; i ++) {
for (auto& e : edges[i]) {
if (!d.same(e.first - 1, e.second - 1)) {
d.unite(e.first - 1, e.second - 1);
ans += i;
}
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
274380 KB |
Output is correct |
2 |
Correct |
209 ms |
276680 KB |
Output is correct |
3 |
Correct |
177 ms |
274364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
235288 KB |
Output is correct |
2 |
Correct |
459 ms |
275020 KB |
Output is correct |
3 |
Correct |
174 ms |
274516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
274344 KB |
Output is correct |
2 |
Correct |
176 ms |
274200 KB |
Output is correct |
3 |
Correct |
173 ms |
274396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
248960 KB |
Output is correct |
2 |
Correct |
269 ms |
276092 KB |
Output is correct |
3 |
Correct |
185 ms |
254088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
240648 KB |
Output is correct |
2 |
Correct |
234 ms |
261340 KB |
Output is correct |
3 |
Correct |
151 ms |
247008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
261224 KB |
Output is correct |
2 |
Correct |
315 ms |
290736 KB |
Output is correct |
3 |
Correct |
180 ms |
252628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
239448 KB |
Output is correct |
2 |
Correct |
285 ms |
285532 KB |
Output is correct |
3 |
Correct |
171 ms |
252056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
286 ms |
286952 KB |
Output is correct |
2 |
Correct |
1419 ms |
483616 KB |
Output is correct |
3 |
Correct |
324 ms |
290168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
295 ms |
286940 KB |
Output is correct |
2 |
Correct |
3020 ms |
574320 KB |
Output is correct |
3 |
Correct |
348 ms |
294112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
276596 KB |
Output is correct |
2 |
Correct |
3301 ms |
570056 KB |
Output is correct |
3 |
Correct |
175 ms |
254500 KB |
Output is correct |