#include<iostream> #include<set> #include<vector> #include<map> struct parowka { bool kr; //0 = K, 1 =R short ind; short kolor; }; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); int n, m, a = 0, b = 0; std::map<char, int> m1; std::vector<char> w; char x; parowka h; std::cin >> n >> m; std::vector<std::map<short, short>> v1(n); std::vector<std::map<short, short>> v2(m); std::vector<parowka> res; res.reserve(n + m); std::vector<std::vector<short>> f1; std::vector<std::vector<short>> f2; f1.resize(n, std::vector<short>(m)); f2.resize(m, std::vector<short>(n)); for (int i = 0; i < n - 1; i++) { for (int j = 0; j < m - 1; j++) { std::cin >> x; if (m1[x] == 0) { m1[x] = m1.size(); w.push_back(x); f1[i][j] = m1[x]; f2[j][i] = m1[x]; v1[i][m1[x]]++; v2[j][m1[x]]++; } else { f1[i][j] = m1[x]; f2[j][i] = m1[x]; v1[i][m1[x]]++; v2[j][m1[x]]++; } } std::cin >> x; if (m1[x] == 0) { m1[x] = m1.size(); w.push_back(x); f1[i][m - 1] = m1[x]; f2[m - 1][i] = m1[x]; v1[i][m1[x]]++; v2[m - 1][m1[x]]++; } else { f1[i][m - 1] = m1[x]; f2[m - 1][i] = m1[x]; v1[i][m1[x]]++; v2[m - 1][m1[x]]++; if (v1[i].size() == 1) { h.kr = 0; a++; h.ind = i; h.kolor = m1[x]; res.push_back(h); } } } for (int j = 0; j < m - 1; j++) { std::cin >> x; if (m1[x] == 0) { m1[x] = m1.size(); w.push_back(x); f1[n - 1][j] = m1[x]; f2[j][n - 1] = m1[x]; v1[n - 1][m1[x]]++; v2[j][m1[x]]++; } else { f1[n - 1][j] = m1[x]; f2[j][n - 1] = m1[x]; v1[n - 1][m1[x]]++; v2[j][m1[x]]++; if (v2[j].size() == 1) { h.kr = 1; b++; h.ind = j; h.kolor = m1[x]; res.push_back(h); } } } std::cin >> x; if (m1[x] == 0) { m1[x] = m1.size(); w.push_back(x); f1[n - 1][m - 1] = m1[x]; f2[m - 1][n - 1] = m1[x]; v1[n - 1][m1[x]]++; v2[m - 1][m1[x]]++; } else { f1[n - 1][m - 1] = m1[x]; f2[m - 1][n - 1] = m1[x]; v1[n - 1][m1[x]]++; v2[m - 1][m1[x]]++; if (v1[n - 1].size() == 1) { h.kr = 0; a++; h.ind = n - 1; h.kolor = m1[x]; res.push_back(h); } if (v2[m - 1].size() == 1) { h.kr = 1; b++; h.ind = m - 1; h.kolor = m1[x]; res.push_back(h); } } std::vector<bool> czyn(n); std::vector<bool> czym(m); std::map<short, short>::iterator itr; for (int i = 0; i < res.size(); i++) { if (a == n or b == m) { break; } if (res[i].kr) { czym[res[i].ind] = true; for (int j = 0; j < n; j++) { if (czyn[j] == false) { itr = v1[j].find(res[i].kolor); if ((*itr).second == 1) { v1[j].erase(itr); if (v1[j].size() == 1) { h.kr = 0; a++; h.ind = j; itr = v1[j].begin(); h.kolor = (*itr).first; res.push_back(h); } } else { (*itr).second--; } } } } else { czyn[res[i].ind] = true; for (int j = 0; j < m; j++) { if (czym[j] == false) { itr = v2[j].find(res[i].kolor); if ((*itr).second == 1) { v2[j].erase(itr); if (v2[j].size() == 1) { h.kr = 1; b++; h.ind = j; itr = v2[j].begin(); h.kolor = (*itr).first; res.push_back(h); } } else { (*itr).second--; } } } } } std::cout << res.size() << "\n"; for (int i = res.size() - 1; i >= 0; i--) { if (res[i].kr) { std::cout << "K "; } else { std::cout << "R "; } std::cout << res[i].ind + 1 << " " << w[res[i].kolor - 1] << "\n"; } 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 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 | #include<iostream> #include<set> #include<vector> #include<map> struct parowka { bool kr; //0 = K, 1 =R short ind; short kolor; }; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); int n, m, a = 0, b = 0; std::map<char, int> m1; std::vector<char> w; char x; parowka h; std::cin >> n >> m; std::vector<std::map<short, short>> v1(n); std::vector<std::map<short, short>> v2(m); std::vector<parowka> res; res.reserve(n + m); std::vector<std::vector<short>> f1; std::vector<std::vector<short>> f2; f1.resize(n, std::vector<short>(m)); f2.resize(m, std::vector<short>(n)); for (int i = 0; i < n - 1; i++) { for (int j = 0; j < m - 1; j++) { std::cin >> x; if (m1[x] == 0) { m1[x] = m1.size(); w.push_back(x); f1[i][j] = m1[x]; f2[j][i] = m1[x]; v1[i][m1[x]]++; v2[j][m1[x]]++; } else { f1[i][j] = m1[x]; f2[j][i] = m1[x]; v1[i][m1[x]]++; v2[j][m1[x]]++; } } std::cin >> x; if (m1[x] == 0) { m1[x] = m1.size(); w.push_back(x); f1[i][m - 1] = m1[x]; f2[m - 1][i] = m1[x]; v1[i][m1[x]]++; v2[m - 1][m1[x]]++; } else { f1[i][m - 1] = m1[x]; f2[m - 1][i] = m1[x]; v1[i][m1[x]]++; v2[m - 1][m1[x]]++; if (v1[i].size() == 1) { h.kr = 0; a++; h.ind = i; h.kolor = m1[x]; res.push_back(h); } } } for (int j = 0; j < m - 1; j++) { std::cin >> x; if (m1[x] == 0) { m1[x] = m1.size(); w.push_back(x); f1[n - 1][j] = m1[x]; f2[j][n - 1] = m1[x]; v1[n - 1][m1[x]]++; v2[j][m1[x]]++; } else { f1[n - 1][j] = m1[x]; f2[j][n - 1] = m1[x]; v1[n - 1][m1[x]]++; v2[j][m1[x]]++; if (v2[j].size() == 1) { h.kr = 1; b++; h.ind = j; h.kolor = m1[x]; res.push_back(h); } } } std::cin >> x; if (m1[x] == 0) { m1[x] = m1.size(); w.push_back(x); f1[n - 1][m - 1] = m1[x]; f2[m - 1][n - 1] = m1[x]; v1[n - 1][m1[x]]++; v2[m - 1][m1[x]]++; } else { f1[n - 1][m - 1] = m1[x]; f2[m - 1][n - 1] = m1[x]; v1[n - 1][m1[x]]++; v2[m - 1][m1[x]]++; if (v1[n - 1].size() == 1) { h.kr = 0; a++; h.ind = n - 1; h.kolor = m1[x]; res.push_back(h); } if (v2[m - 1].size() == 1) { h.kr = 1; b++; h.ind = m - 1; h.kolor = m1[x]; res.push_back(h); } } std::vector<bool> czyn(n); std::vector<bool> czym(m); std::map<short, short>::iterator itr; for (int i = 0; i < res.size(); i++) { if (a == n or b == m) { break; } if (res[i].kr) { czym[res[i].ind] = true; for (int j = 0; j < n; j++) { if (czyn[j] == false) { itr = v1[j].find(res[i].kolor); if ((*itr).second == 1) { v1[j].erase(itr); if (v1[j].size() == 1) { h.kr = 0; a++; h.ind = j; itr = v1[j].begin(); h.kolor = (*itr).first; res.push_back(h); } } else { (*itr).second--; } } } } else { czyn[res[i].ind] = true; for (int j = 0; j < m; j++) { if (czym[j] == false) { itr = v2[j].find(res[i].kolor); if ((*itr).second == 1) { v2[j].erase(itr); if (v2[j].size() == 1) { h.kr = 1; b++; h.ind = j; itr = v2[j].begin(); h.kolor = (*itr).first; res.push_back(h); } } else { (*itr).second--; } } } } } std::cout << res.size() << "\n"; for (int i = res.size() - 1; i >= 0; i--) { if (res[i].kr) { std::cout << "K "; } else { std::cout << "R "; } std::cout << res[i].ind + 1 << " " << w[res[i].kolor - 1] << "\n"; } return 0; } |