#include <iostream> #include <map> #include <queue> #include <vector> struct Row { int index; std::map<char, int> colorCounts; }; struct CompareRow { bool operator()(const Row &a, const Row &b) const { return a.colorCounts.size() > b.colorCounts.size(); } }; typedef std::priority_queue<Row, std::vector<Row>, CompareRow> RowQueue; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n, m; std::cin >> n >> m; std::vector<Row> xRows(m); std::vector<Row> yRows(n); for (int i = 0; i < m; i++) { xRows[i].index = i + 1; } for (int i = 0; i < n; i++) { yRows[i].index = i + 1; } for (int i = 0; i < n; i++) { char c; for (int j = 0; j < m; j++) { std::cin >> c; xRows[j].colorCounts[c]++; yRows[i].colorCounts[c]++; } } RowQueue xQueue(xRows.begin(), xRows.end()); RowQueue yQueue(yRows.begin(), yRows.end()); std::vector<std::tuple<bool, int, char>> results; results.reserve(n + m); while (!xQueue.empty() && !yQueue.empty()) { if (!xQueue.empty()) { Row xRow = xQueue.top(); if (xRow.colorCounts.size() == 1) { auto colorToRemove = xRow.colorCounts.begin()->first; std::vector<Row> newYRows; newYRows.reserve(yQueue.size()); while (!yQueue.empty()) { newYRows.push_back(yQueue.top()); yQueue.pop(); } for (auto &yRow : newYRows) { yRow.colorCounts[colorToRemove]--; if (yRow.colorCounts[colorToRemove] == 0) { yRow.colorCounts.erase(colorToRemove); } } for (auto &yRow : newYRows) { if (yRow.colorCounts.size() > 0) { yQueue.push(yRow); } } results.push_back(std::make_tuple(true, xRow.index, colorToRemove)); xQueue.pop(); } } if (!yQueue.empty()) { Row yRow = yQueue.top(); if (yRow.colorCounts.size() == 1) { auto colorToRemove = yRow.colorCounts.begin()->first; std::vector<Row> newXRows; newXRows.reserve(xQueue.size()); while (!xQueue.empty()) { newXRows.push_back(xQueue.top()); xQueue.pop(); } for (auto &xRow : newXRows) { xRow.colorCounts[colorToRemove]--; if (xRow.colorCounts[colorToRemove] == 0) { xRow.colorCounts.erase(colorToRemove); } } for (auto &xRow : newXRows) { if (xRow.colorCounts.size() > 0) { xQueue.push(xRow); } } results.push_back(std::make_tuple(false, yRow.index, colorToRemove)); yQueue.pop(); } } } std::cout << results.size() << std::endl; for (auto it = results.rbegin(); it != results.rend(); it++) { if (std::get<0>(*it)) { std::cout << "K " << std::get<1>(*it) << " " << std::get<2>(*it) << std::endl; } else { std::cout << "R " << std::get<1>(*it) << " " << std::get<2>(*it) << std::endl; } } return 0; }
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 | #include <iostream> #include <map> #include <queue> #include <vector> struct Row { int index; std::map<char, int> colorCounts; }; struct CompareRow { bool operator()(const Row &a, const Row &b) const { return a.colorCounts.size() > b.colorCounts.size(); } }; typedef std::priority_queue<Row, std::vector<Row>, CompareRow> RowQueue; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n, m; std::cin >> n >> m; std::vector<Row> xRows(m); std::vector<Row> yRows(n); for (int i = 0; i < m; i++) { xRows[i].index = i + 1; } for (int i = 0; i < n; i++) { yRows[i].index = i + 1; } for (int i = 0; i < n; i++) { char c; for (int j = 0; j < m; j++) { std::cin >> c; xRows[j].colorCounts[c]++; yRows[i].colorCounts[c]++; } } RowQueue xQueue(xRows.begin(), xRows.end()); RowQueue yQueue(yRows.begin(), yRows.end()); std::vector<std::tuple<bool, int, char>> results; results.reserve(n + m); while (!xQueue.empty() && !yQueue.empty()) { if (!xQueue.empty()) { Row xRow = xQueue.top(); if (xRow.colorCounts.size() == 1) { auto colorToRemove = xRow.colorCounts.begin()->first; std::vector<Row> newYRows; newYRows.reserve(yQueue.size()); while (!yQueue.empty()) { newYRows.push_back(yQueue.top()); yQueue.pop(); } for (auto &yRow : newYRows) { yRow.colorCounts[colorToRemove]--; if (yRow.colorCounts[colorToRemove] == 0) { yRow.colorCounts.erase(colorToRemove); } } for (auto &yRow : newYRows) { if (yRow.colorCounts.size() > 0) { yQueue.push(yRow); } } results.push_back(std::make_tuple(true, xRow.index, colorToRemove)); xQueue.pop(); } } if (!yQueue.empty()) { Row yRow = yQueue.top(); if (yRow.colorCounts.size() == 1) { auto colorToRemove = yRow.colorCounts.begin()->first; std::vector<Row> newXRows; newXRows.reserve(xQueue.size()); while (!xQueue.empty()) { newXRows.push_back(xQueue.top()); xQueue.pop(); } for (auto &xRow : newXRows) { xRow.colorCounts[colorToRemove]--; if (xRow.colorCounts[colorToRemove] == 0) { xRow.colorCounts.erase(colorToRemove); } } for (auto &xRow : newXRows) { if (xRow.colorCounts.size() > 0) { xQueue.push(xRow); } } results.push_back(std::make_tuple(false, yRow.index, colorToRemove)); yQueue.pop(); } } } std::cout << results.size() << std::endl; for (auto it = results.rbegin(); it != results.rend(); it++) { if (std::get<0>(*it)) { std::cout << "K " << std::get<1>(*it) << " " << std::get<2>(*it) << std::endl; } else { std::cout << "R " << std::get<1>(*it) << " " << std::get<2>(*it) << std::endl; } } return 0; } |