This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Source: https://oj.uz/problem/view/COCI17_sirni
//
#include "bits/stdc++.h"
using namespace std;
#define s second
#define f first
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<int> vi;
#define FOR(i, a, b) for (int i = (a); i<b; i++)
bool ckmin(int& a, int b){ return b < a ? a = b, true : false; }
bool ckmax(int& a, int b){ return b > a ? a = b, true : false; }
const int P = 1e7 + 10;
vpii ed[P];
bool vals[P];
struct DSU {
vector<int> e;
DSU(int N) { e = vector<int>(N, -1); }
// get representive component (uses path compression)
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool same_set(int a, int b) { return get(a) == get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int 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;
}
};
DSU dsu(P);
int nxt[P];
set<int> uni;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
FOR(i, 1, n + 1) {
int k;
cin >> k;
vals[k]=true;
uni.insert(k);
}
int largest = -1;
int cur = largest;
for (int i = P-1; i >= 0; i--) {
if (vals[i]) largest=i;
// if (largest != -1) cout << largest << endl;
nxt[i] = largest;
}
for (auto cur: uni) {
for (int e = cur; e < P; e += cur) {
int val = nxt[e];
if (e == cur) val = nxt[e+1];
if (val == -1) break;
if (val < e + cur) ed[val % cur].pb({cur, val});
// cout << cur << val << endl;
// if (it == uni.begin()) continue;
// int val = *prev(it, 1);
// while (val >= e) {
// cout << cur << val << endl;
// ed.pb({val - e, {cur, val}});
// ub = (e + val) / 2;
// if (val == e) break;
// it = uni.upper_bound(ub);
// if (it == uni.begin()) break;
// val = *prev(it, 1);
// }
}
}
int sz = 0;
FOR(c, 0, P) {
for (auto e: ed[c])
if (dsu.unite(e.f, e.s)) sz += c;
}
cout << sz << endl;
}
Compilation message (stderr)
sirni.cpp: In function 'int main()':
sirni.cpp:73:6: warning: unused variable 'cur' [-Wunused-variable]
73 | int cur = largest;
| ^~~
# | 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... |