# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1124803 | KhanhDang | Sirni (COCI17_sirni) | C++20 | 970 ms | 107000 KiB |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll, ll>
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define Unique(v) sort(all(v)); (v).erase(unique(all(v)), (v).end());
const ll N = 1e5+5;
ll n, ans;
ll par[N];
map<ll, ll> f;
vector<ll> pos;
struct Edge {
ll u, v, w;
};
vector<Edge> e;
ll findSet(ll u) {
return (u == par[u] ? u : par[u] = findSet(par[u]));
}
void unionSet(ll u, ll v) {
u = findSet(u);
v = findSet(v);
par[u] = v;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define task "task"
if (fopen(task".INP", "r")) {
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
if (fopen("task.INP", "r")) {
freopen("task.INP", "r", stdin);
freopen("task.OUT", "w", stdout);
}
cin>>n;
for (int i = 1; i <= n; i++) par[i] = i;
for (int i = 1; i <= n; i++) {
ll x; cin>>x; pos.push_back(x);
if (f[x] == 0) f[x] = i;
else unionSet(f[x], i);
}
Unique(pos);
for (auto x : pos) {
for (int i = 1; ; i++) {
auto it = lower_bound(all(pos), x * i);
if (it == pos.end()) break;
else e.push_back({f[x], f[*it], (*it) % x});
}
}
sort(all(e), [&] (Edge a, Edge b) {
return a.w < b.w;
});
for (auto x : e)
if (findSet(x.u) != findSet(x.v)) {
unionSet(x.u, x.v);
ans += x.w;
}
cout<<ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |