#include <iostream>
#include <cassert>
#include <vector>
#include <algorithm>
using std::cout;
using std::endl;
using std::vector;
using std::pair;
class DisjointSets {
private:
vector<int> parents;
vector<int> sizes;
public:
DisjointSets(int size) : parents(size), sizes(size, 1) {
for (int i = 0; i < size; i++) {
parents[i] = i;
}
}
int get_ultimate(int n) {
return parents[n] == n ? n : (parents[n] = get_ultimate(parents[n]));
}
bool link(int n1, int n2) {
n1 = get_ultimate(n1);
n2 = get_ultimate(n2);
if (n1 == n2) {
return false;
}
if (sizes[n1] < sizes[n2]) {
std::swap(n1, n2);
}
sizes[n1] += sizes[n2];
parents[n2] = n1;
return true;
}
};
/**
* https://oj.uz/problem/view/COCI17_sirni
* 4
* 2
* 6
* 3
* 11 should output 1
*/
int main() {
int card_num;
std::cin >> card_num;
vector<int> cards(card_num);
for (int& c : cards) {
std::cin >> c;
assert(c >= 1);
}
std::sort(cards.begin(), cards.end());
// we can erase the dupes bc modding them with the original one = 0
cards.erase(std::unique(cards.begin(), cards.end()), cards.end());
int largest = *std::max_element(cards.begin(), cards.end());
vector<int> next_largest(largest + 1);
for (int i = 0; i < cards.size(); i++) {
next_largest[cards[i]] = i;
}
for (int c = largest; c >= 0; c--) {
// if this isn't assigned yet, assign it the previous one
if (next_largest[c] == 0) {
next_largest[c] = next_largest[c + 1];
}
}
vector<pair<int, pair<int, int>>> good_links;
for (int i = 0; i < cards.size() - 1; i++) {
good_links.push_back({cards[i + 1] % cards[i], {i, i + 1}});
for (int at = 2 * cards[i]; at <= largest; at += cards[i]) {
int good_mod = next_largest[at];
good_links.push_back({cards[good_mod] % cards[i], {i, good_mod}});
}
}
std::sort(good_links.begin(), good_links.end());
long long total_cost = 0;
DisjointSets linked_cards(cards.size());
for (const auto& [cost, to_link] : good_links) {
total_cost += cost * linked_cards.link(to_link.first, to_link.second);
}
cout << total_cost << endl;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:63:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int i = 0; i < cards.size(); i++) {
| ~~^~~~~~~~~~~~~~
sirni.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 0; i < cards.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
39460 KB |
Output is correct |
2 |
Correct |
301 ms |
88608 KB |
Output is correct |
3 |
Correct |
41 ms |
39728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Runtime error |
936 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
39492 KB |
Output is correct |
2 |
Correct |
40 ms |
39364 KB |
Output is correct |
3 |
Correct |
48 ms |
39676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
222 ms |
29952 KB |
Output is correct |
2 |
Correct |
719 ms |
54992 KB |
Output is correct |
3 |
Correct |
326 ms |
54672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
7508 KB |
Output is correct |
2 |
Correct |
357 ms |
54356 KB |
Output is correct |
3 |
Correct |
191 ms |
28008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
407 ms |
54664 KB |
Output is correct |
2 |
Correct |
966 ms |
103868 KB |
Output is correct |
3 |
Correct |
275 ms |
30124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
7676 KB |
Output is correct |
2 |
Correct |
914 ms |
103808 KB |
Output is correct |
3 |
Correct |
285 ms |
29992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
285 ms |
65248 KB |
Output is correct |
2 |
Runtime error |
1142 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
336 ms |
65340 KB |
Output is correct |
2 |
Runtime error |
966 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
42860 KB |
Output is correct |
2 |
Runtime error |
1109 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |