# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1041336 |
2024-08-01T22:02:26 Z |
sssamui |
Sirni (COCI17_sirni) |
C++17 |
|
1002 ms |
747208 KB |
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::pair;
using std::vector;
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;
}
};
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:52:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i = 0; i < cards.size(); i++) { next_largest[cards[i]] = i; }
| ~~^~~~~~~~~~~~~~
sirni.cpp:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i = 0; i < cards.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
274132 KB |
Output is correct |
2 |
Correct |
168 ms |
302588 KB |
Output is correct |
3 |
Correct |
107 ms |
273496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
689 ms |
668504 KB |
Output is correct |
3 |
Correct |
100 ms |
275064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
274452 KB |
Output is correct |
2 |
Correct |
114 ms |
273916 KB |
Output is correct |
3 |
Correct |
104 ms |
274452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
38924 KB |
Output is correct |
2 |
Correct |
89 ms |
70596 KB |
Output is correct |
3 |
Correct |
71 ms |
50680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
29736 KB |
Output is correct |
2 |
Correct |
68 ms |
51500 KB |
Output is correct |
3 |
Correct |
43 ms |
25336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
50304 KB |
Output is correct |
2 |
Correct |
103 ms |
87232 KB |
Output is correct |
3 |
Correct |
66 ms |
48264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
9920 KB |
Output is correct |
2 |
Correct |
108 ms |
85616 KB |
Output is correct |
3 |
Correct |
56 ms |
52656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
156 ms |
289132 KB |
Output is correct |
2 |
Correct |
739 ms |
632872 KB |
Output is correct |
3 |
Correct |
160 ms |
291876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
293160 KB |
Output is correct |
2 |
Correct |
1002 ms |
747208 KB |
Output is correct |
3 |
Correct |
257 ms |
348508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
276836 KB |
Output is correct |
2 |
Correct |
845 ms |
634976 KB |
Output is correct |
3 |
Correct |
57 ms |
52884 KB |
Output is correct |