#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define is insert
#define ll long long
#define V vector
#define MS multiset
#define PL pair<ll, ll>
#define F first
#define S second
#define PQ priority_queue
#define f0r(i, begin, end) for (ll i = begin; i < end; i ++)
#define For(i, end, begin) for (ll i = end; i > begin; i --)
#define all(x) x.begin(), x.end()
#define INF 1000000000000000000
#define inf 1000000000
#define MOD 1000000009
#define len(x) (ll)x.size()
#define fileread(file) ifstream fin; fin.open((string)file + ".in"); ofstream fout; fout.open((string)file + ".out")
#define fastio ios_base::sync_with_stdio(0); cin.tie(nullptr)
template<typename T> istream& operator>>(istream& in, V<T>& a) {for(auto &x : a) in >> x; return in;};
template<typename T> ostream& operator<<(ostream& out, V<T>& a) {for(auto &x : a) out << x << ' '; return out;};
struct DSU {
vector<ll> e;
DSU(ll N) { e = vector<ll>(N, -1); }
// get representive component (uses path compression)
ll get(ll x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool same_set(ll a, ll b) { return get(a) == get(b); }
ll size(ll x) { return -e[get(x)]; }
bool unite(ll x, ll y) { // union by size
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y];
e[y] = x;
return true;
}
};
ll N, ans = 0;
set<ll> nums;
map<ll, ll> decomp;
vector<tuple<ll, ll, ll>> edges;
void krustal () {
ll tot = N - 1;
DSU dsu(N);
sort(all(edges));
for (auto e : edges) {
if (dsu.unite(get<1>(e), get<2>(e))) {
ans += get<0>(e);
if ((--tot) == 0) break;
}
}
}
int main () {
cin >> N;
DSU dsu(N);
f0r (i, 0, N) {
ll a; cin >> a;
nums.is(a);
}
ll i = 0;
for (auto n : nums) {
decomp[n] = (i ++);
}
for (auto n : nums) {
auto pos = nums.lower_bound(n + 1);
ll factor = 1;
while (pos != nums.end()) {
if ((*pos) / n > factor) {
factor = (*pos) / n;
}
edges.pb({(*pos) % n, decomp[n], decomp[*pos]});
pos = nums.lower_bound((++factor) * n);
}
}
krustal();
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
664 KB |
Output is correct |
2 |
Correct |
37 ms |
6632 KB |
Output is correct |
3 |
Correct |
3 ms |
884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
10 ms |
2052 KB |
Output is correct |
3 |
Correct |
3 ms |
920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
400 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
638 ms |
36900 KB |
Output is correct |
2 |
Correct |
1895 ms |
109844 KB |
Output is correct |
3 |
Correct |
691 ms |
59476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
8256 KB |
Output is correct |
2 |
Correct |
624 ms |
101124 KB |
Output is correct |
3 |
Correct |
544 ms |
35484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1295 ms |
110764 KB |
Output is correct |
2 |
Correct |
2531 ms |
208844 KB |
Output is correct |
3 |
Correct |
694 ms |
61324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
16980 KB |
Output is correct |
2 |
Correct |
2012 ms |
206856 KB |
Output is correct |
3 |
Correct |
637 ms |
60172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
684 ms |
62156 KB |
Output is correct |
2 |
Runtime error |
4475 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
686 ms |
62044 KB |
Output is correct |
2 |
Runtime error |
4203 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
9072 KB |
Output is correct |
2 |
Runtime error |
4230 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |