// PA2024 runda 4C - https://sio2.mimuw.edu.pl/c/pa-2024-1/p/lam/ // 9:00 //-std=c++20 #include<iostream> #include <cstddef> #include <algorithm> #include<vector> #include <random> // Added for random number generation #include <list> #include<map> using I = int64_t; struct Command { char color; char type; I x; }; struct Verifier { std::vector<std::vector<char>> matrix; void execute(I n, I m, std::list<Command> &commands) { matrix.resize(n); for (int i = 0; i < n; ++i) { matrix[i].resize(m); } for (const auto &item: commands) { if (item.type == 'R') { for (int j = 0; j < m; ++j) { matrix[item.x][j] = item.color; } } else { for (int i = 0; i < n; i++) { matrix[i][item.x] = item.color; } } } } void compare(std::vector<std::vector<char>> &other) { if (matrix != other) { std::cerr << "Result is different than expected"; } } void print(I n, I m) { std::cout << n << " " << m << '\n'; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { std::cout << matrix[i][j]; } std::cout << std::endl; } } }; struct E { I index; std::map<char, I> color_stats; E(I i) : index(i) {}; void add(char color) { if (color_stats.contains(color)) { color_stats[color]++; } else { color_stats[color] = 1; } } void remove(char color) { if (color_stats.contains(color)) { if (color_stats[color] == 1) { color_stats.erase(color); } else { color_stats[color]--; } } } }; struct Lam { I n, m; // <1, 2000> std::vector<std::vector<char>> matrix; std::list<E *> columns; std::list<E *> rows; std::list<Command> results; void run() { input_data(); prepare_stats(); solve(); print_result(); //Verifier v; //v.execute(n, m, results); //v.compare(matrix); } void prepare_stats() { for (I i = 0; i < n; i++) { E *e = new E(i); for (int j = 0; j < m; ++j) { e->add(matrix[i][j]); } rows.push_back(e); } for (I j = 0; j < m; j++) { E *e = new E(j); for (int i = 0; i < n; ++i) { e->add(matrix[i][j]); } columns.push_back(e); } } void solve() { bool row = true; while (!columns.empty() && !rows.empty()) { if (row) { step(rows, columns, 'R'); } else { step(columns, rows, 'K'); } row = !row; } } void step(std::list<E *> &first, std::list<E *> &second, char type) { auto it = first.begin(); while (it != first.end()) { if ((*it)->color_stats.size() == 1) { Command c; c.color = (*it)->color_stats.begin()->first; c.type = type; c.x = (*it)->index; for (auto &item: second) { item->remove(c.color); } results.push_front(c); delete (*it); it = first.erase(it); } else { it++; } } } void print_result() { std::cout << results.size() << '\n'; for (const auto &item: results) { std::cout << item.type << " " << item.x + 1 << " " << item.color << '\n'; } } void input_data() { std::cin >> n >> m; matrix.resize(n); for (I i = 0; i < n; ++i) { matrix[i].resize(m); for (I j = 0; j < m; j++) { std::cin >> matrix[i][j]; } } } }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); Lam l; l.run(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | // PA2024 runda 4C - https://sio2.mimuw.edu.pl/c/pa-2024-1/p/lam/ // 9:00 //-std=c++20 #include<iostream> #include <cstddef> #include <algorithm> #include<vector> #include <random> // Added for random number generation #include <list> #include<map> using I = int64_t; struct Command { char color; char type; I x; }; struct Verifier { std::vector<std::vector<char>> matrix; void execute(I n, I m, std::list<Command> &commands) { matrix.resize(n); for (int i = 0; i < n; ++i) { matrix[i].resize(m); } for (const auto &item: commands) { if (item.type == 'R') { for (int j = 0; j < m; ++j) { matrix[item.x][j] = item.color; } } else { for (int i = 0; i < n; i++) { matrix[i][item.x] = item.color; } } } } void compare(std::vector<std::vector<char>> &other) { if (matrix != other) { std::cerr << "Result is different than expected"; } } void print(I n, I m) { std::cout << n << " " << m << '\n'; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { std::cout << matrix[i][j]; } std::cout << std::endl; } } }; struct E { I index; std::map<char, I> color_stats; E(I i) : index(i) {}; void add(char color) { if (color_stats.contains(color)) { color_stats[color]++; } else { color_stats[color] = 1; } } void remove(char color) { if (color_stats.contains(color)) { if (color_stats[color] == 1) { color_stats.erase(color); } else { color_stats[color]--; } } } }; struct Lam { I n, m; // <1, 2000> std::vector<std::vector<char>> matrix; std::list<E *> columns; std::list<E *> rows; std::list<Command> results; void run() { input_data(); prepare_stats(); solve(); print_result(); //Verifier v; //v.execute(n, m, results); //v.compare(matrix); } void prepare_stats() { for (I i = 0; i < n; i++) { E *e = new E(i); for (int j = 0; j < m; ++j) { e->add(matrix[i][j]); } rows.push_back(e); } for (I j = 0; j < m; j++) { E *e = new E(j); for (int i = 0; i < n; ++i) { e->add(matrix[i][j]); } columns.push_back(e); } } void solve() { bool row = true; while (!columns.empty() && !rows.empty()) { if (row) { step(rows, columns, 'R'); } else { step(columns, rows, 'K'); } row = !row; } } void step(std::list<E *> &first, std::list<E *> &second, char type) { auto it = first.begin(); while (it != first.end()) { if ((*it)->color_stats.size() == 1) { Command c; c.color = (*it)->color_stats.begin()->first; c.type = type; c.x = (*it)->index; for (auto &item: second) { item->remove(c.color); } results.push_front(c); delete (*it); it = first.erase(it); } else { it++; } } } void print_result() { std::cout << results.size() << '\n'; for (const auto &item: results) { std::cout << item.type << " " << item.x + 1 << " " << item.color << '\n'; } } void input_data() { std::cin >> n >> m; matrix.resize(n); for (I i = 0; i < n; ++i) { matrix[i].resize(m); for (I j = 0; j < m; j++) { std::cin >> matrix[i][j]; } } } }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); Lam l; l.run(); } |