#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::pair;
using std::vector;
// BeginCodeSnip{DSU (from the module)}
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 find(int n) {
return parents[n] == n ? n : (parents[n] = find(parents[n]));
}
bool unite(int n1, int n2) {
n1 = find(n1);
n2 = find(n2);
if (n1 == n2) { return false; }
if (sizes[n1] < sizes[n2]) { std::swap(n1, n2); }
sizes[n1] += sizes[n2];
parents[n2] = n1;
return true;
}
};
// EndCodeSnip
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 = cards.back(); // since we sorted the cards already
// next_largest[i] contains the index of lowest card value that's >= i
vector<int> next_largest(largest + 1, -1);
for (int i = 0; i < cards.size(); i++) { next_largest[cards[i]] = i; }
for (int c = largest - 1; c >= 0; c--) {
// if this isn't assigned yet, assign it the previous one
if (next_largest[c] == -1) { next_largest[c] = next_largest[c + 1]; }
}
vector<vector<pair<int, int>>> good_links(largest + 1);
for (int i = 0; i < cards.size() - 1; i++) {
// get all relevant cards this card could be connected to
good_links[cards[i + 1] % cards[i]].push_back({i, i + 1});
for (int at = 2 * cards[i]; at <= largest; at += cards[i]) {
int good_mod = next_largest[at];
good_links[cards[good_mod] % cards[i]].push_back({i, good_mod});
}
}
long long total_cost = 0;
DisjointSets linked_cards(cards.size());
for (int c = 0; c <= largest; c++) {
for (const pair<int, int> &link : good_links[c]) {
bool result = linked_cards.unite(link.first, link.second);
total_cost += c * result;
}
}
cout << total_cost << endl;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:54:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int i = 0; i < cards.size(); i++) { next_largest[cards[i]] = i; }
| ~~^~~~~~~~~~~~~~
sirni.cpp:61:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i = 0; i < cards.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
274256 KB |
Output is correct |
2 |
Correct |
180 ms |
302452 KB |
Output is correct |
3 |
Correct |
157 ms |
273548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
775 ms |
668528 KB |
Output is correct |
3 |
Correct |
134 ms |
275028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
274260 KB |
Output is correct |
2 |
Correct |
129 ms |
273748 KB |
Output is correct |
3 |
Correct |
128 ms |
274512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
38280 KB |
Output is correct |
2 |
Correct |
110 ms |
66360 KB |
Output is correct |
3 |
Correct |
68 ms |
48824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
29988 KB |
Output is correct |
2 |
Correct |
69 ms |
51028 KB |
Output is correct |
3 |
Correct |
47 ms |
24664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
49712 KB |
Output is correct |
2 |
Correct |
122 ms |
83408 KB |
Output is correct |
3 |
Correct |
72 ms |
46740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
9660 KB |
Output is correct |
2 |
Correct |
131 ms |
84116 KB |
Output is correct |
3 |
Correct |
88 ms |
48568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
174 ms |
288288 KB |
Output is correct |
2 |
Correct |
834 ms |
632388 KB |
Output is correct |
3 |
Correct |
193 ms |
291816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
292324 KB |
Output is correct |
2 |
Correct |
1146 ms |
746428 KB |
Output is correct |
3 |
Correct |
329 ms |
348536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
276828 KB |
Output is correct |
2 |
Correct |
1099 ms |
634456 KB |
Output is correct |
3 |
Correct |
75 ms |
51132 KB |
Output is correct |