This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <queue>
#define MAXN 100000
#define MAXP 1e7
using namespace std;
typedef pair<int, int> pi;
int N;
int Cards[MAXN + 5];
set<int> cards;
priority_queue<pair<int, pi>> PQ; // PQ of edges
int parent[MAXN + 1]; // initialize parent[i] = i
int findRoot(int a) {
if (parent[a] == a) {
return a;
}
return parent[a] = findRoot(parent[a]);
}
bool isConnected(int a, int b) {
return findRoot(a) == findRoot(b);
}
// fastest method, check if 2 nodes are connected, check size of group
int sz[MAXN + 1]; // initialize size[i] = 1
int depth[MAXN + 1]; // initialize depth[i] = 1
void join(int a, int b) {
a = findRoot(a);
b = findRoot(b);
if (a == b) {
return;
}
if (depth[a] < depth[b]) {
parent[a] = b;
sz[b] += sz[a];
}
else {
parent[b] = a;
depth[a] = max(depth[a], depth[b] + 1);
sz[a] += sz[b];
}
}
int card[MAXN + 5]; // value for each card ID
map<int, int> ID; // card value to card ID
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> Cards[i];
cards.insert(Cards[i]);
}
fill(depth, depth + MAXN + 1, 1);
fill(sz, sz + MAXN + 1, 1);
int i = 1;
for (int c : cards) {
ID[c] = i;
card[i] = c;
parent[i] = i;
i++;
}
for (int c : cards) {
int mult = c; // multiple
auto it = cards.upper_bound(mult);
while (it != cards.end()) {
if (*it - mult > c) mult += (*it - mult) / c * c; // c can be increased
PQ.push({ -(*it - mult), {ID[c], ID[*it] } }); // negative weight to go from least to greatest
mult += c;
if (cards.count(mult)) {
join(ID[c], ID[mult]); // multiple of c is a card, can join with no edge weight
}
it = cards.upper_bound(mult);
}
}
int ans = 0;
while (!PQ.empty()) {
auto curr = PQ.top(); PQ.pop();
int w = -curr.first;
int f = curr.second.first;
int s = curr.second.second;
if (!isConnected(f, s)) {
join(f, s);
ans += w;
}
if (sz[1] == cards.size()) break;
}
cout << ans;
}
Compilation message (stderr)
sirni.cpp: In function 'int main()':
sirni.cpp:89:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | if (sz[1] == cards.size()) break;
| ~~~~~~^~~~~~~~~~~~~~~
# | 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... |