#include <bits/stdc++.h>
using namespace std;
using namespace std::placeholders;
// #define rand __rand
// mt19937 rand(chrono::system_clock::now().time_since_epoch().count()); // or mt19937_64
#define llong long long
#define xx first
#define yy second
#define len(x) ((int)x.size())
#define rep(i,n) for (int i = -1; ++ i < n; )
#define rep1(i,n) for (int i = 0; i ++ < n; )
#define all(x) x.begin(), x.end()
template<class T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
#define maxn 101010
#define maxval ((llong)1e7+10)
int n;
int a[maxn];
int dsu[maxn];
int n_set;
int findp(int u) { return u == dsu[u] ? u : dsu[u] = findp(dsu[u]); }
void join(int u, int v) {
u = findp(u);
v = findp(v);
if (u == v) return ;
if (rand() & 1) swap(u, v);
dsu[u] = v;
--n_set;
}
int lower_b[maxval] = {0};
vector<pair<int, int>> group[maxval];
int main(void) {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
rep(i, n) cin >> a[i];
sort(a, a + n);
n = (int)(unique(a, a + n) - a);
int f = 0;
rep(i, n) {
for (; f <= a[i]; ++f) lower_b[f] = i;
}
// clog << "COOL" << endl;
min_priority_queue<tuple<int, int, int>> pq;
rep(i, n) {
// clog << i << ' ' << a[i] << endl;
for (int k = 1; k * a[i] <= a[n - 1]; ++k) {
int t = lower_b[k * a[i]];
if (t != n and a[t] == a[i]) ++t;
if (t == n) break;
group[a[t] % a[i]].emplace_back(i, t);
k = a[t] / a[i];
}
}
// clog << "nice" << endl;
n_set = n;
rep1(i, n) dsu[i] = i;
llong ans = 0;
for (int cost = 0; cost < maxval and n_set != 1; ++cost) {
for (auto i: group[cost]) {
if (n_set == 1) break;
int src = i.xx, cur = i.yy;
if (findp(src) != findp(cur)) {
ans += cost;
join(src, cur);
}
int div = a[cur] / a[src];
++cur;
if (cur == n or a[cur] / a[src] != div) continue;
group[a[cur] % a[src]].emplace_back(src, cur);
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
259 ms |
274720 KB |
Output is correct |
2 |
Correct |
279 ms |
276600 KB |
Output is correct |
3 |
Correct |
257 ms |
274552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
235512 KB |
Output is correct |
2 |
Correct |
262 ms |
275216 KB |
Output is correct |
3 |
Correct |
255 ms |
274728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
255 ms |
274524 KB |
Output is correct |
2 |
Correct |
257 ms |
274408 KB |
Output is correct |
3 |
Correct |
257 ms |
274680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
430 ms |
257292 KB |
Output is correct |
2 |
Correct |
720 ms |
289832 KB |
Output is correct |
3 |
Correct |
394 ms |
255704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
243 ms |
241272 KB |
Output is correct |
2 |
Correct |
492 ms |
263192 KB |
Output is correct |
3 |
Correct |
401 ms |
257320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
664 ms |
277008 KB |
Output is correct |
2 |
Correct |
771 ms |
296324 KB |
Output is correct |
3 |
Correct |
392 ms |
255576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
287 ms |
242624 KB |
Output is correct |
2 |
Correct |
741 ms |
296636 KB |
Output is correct |
3 |
Correct |
384 ms |
254796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
564 ms |
293508 KB |
Output is correct |
2 |
Correct |
3933 ms |
487904 KB |
Output is correct |
3 |
Correct |
603 ms |
295920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
510 ms |
288800 KB |
Output is correct |
2 |
Execution timed out |
5136 ms |
531360 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
309 ms |
278768 KB |
Output is correct |
2 |
Execution timed out |
5131 ms |
528856 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |