// Daniel Grzegorzewski // while (clock()<=69*CLOCKS_PER_SEC) #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> // #pragma GCC optimize("Ofast") // #pragma GCC target("avx,avx2,fma") #define MP make_pair #define PB push_back #define ST first #define ND second using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) //X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<PII> VII; typedef long long LL; void init_ios() { ios_base::sync_with_stdio(0); cin.tie(0); } const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count(); mt19937_64 rng(SEED); const int N = 2002; int n, m, cntRow[N], cntCol[N]; string s[N]; int row[N][26], col[N][26]; bool visC[N], visR[N]; int main() { init_ios(); // cin >> n >> m; // for (int i = 0; i < n; ++i) // cin >> s[i]; // n = m = 2000; // // cout<<"2000 2000\n"; // for (int i = 0; i < 2000; ++i) { // s[i].resize(2005); // for (int j = 0; j < 2000; ++j) { // int el = rng()%26; // s[i][j] = (char)(el+'A'); // // cout<<(char)(el+'A'); // } // // cout<<"\n"; // } cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> s[i]; for (int j = 0; j < m; ++j) { int el = (int)(s[i][j]-'A'); if (row[i][el] == 0) ++cntRow[i]; ++row[i][el]; if (col[j][el] == 0) ++cntCol[j]; col[j][el]++; } } set<pair<int, pair<char, int>>> se; for (int i = 0; i < n; ++i) { // cout<<"i = "<<i<<", "<<cntRow[i]<<"\n"; se.insert({cntRow[i], {'R', i}}); } for (int j = 0; j < m; ++j) { // cout<<"j = "<<j<<", "<<cntCol[j]<<"\n"; se.insert({cntCol[j], {'K', j}}); } vector<pair<char, pair<int, char>>> res; // cout<<"elo\n"; while (!se.empty()) { auto el = *se.begin(); se.erase(se.begin()); // cout<<el.ST<<", "<<el.ND.ST<<", "<<el.ND.ND<<"\n"; char qwe; bool bad = false; if (el.ND.ST == 'R') { visR[el.ND.ND] = true; if (cntRow[el.ND.ND] == 0) bad = true; else { for (int e = 0; e < 26; ++e) if (row[el.ND.ND][e] > 0) { qwe = (char)(e+'A'); break; } } // cout<<"qwe = "<<qwe<<"\n"; for (int j = 0; j < m; ++j) { if (visC[j]) continue; if (col[j][qwe-'A'] > 0) --col[j][qwe-'A']; if (col[j][qwe-'A'] == 0) { se.erase({cntCol[j], {'K', j}}); --cntCol[j]; se.insert({cntCol[j], {'K', j}}); } } } else { visC[el.ND.ND] = true; if (col[el.ND.ND] == 0) bad = true; else { for (int e = 0; e < 26; ++e) if (col[el.ND.ND][e] > 0) { qwe = (char)(e+'A'); break; } } // cout<<"qqwe = "<<qwe<<"\n"; for (int i = 0; i < n; ++i) { if (visR[i]) continue; if (row[i][qwe-'A'] > 0) --row[i][qwe-'A']; if (row[i][qwe-'A'] == 0) { se.erase({cntRow[i], {'R', i}}); --cntRow[i]; se.insert({cntRow[i], {'R', i}}); } } } if (!bad) res.PB({el.ND.ST, {el.ND.ND+1, qwe}}); } cout<<res.size()<<"\n"; while (res.size()) { auto el = res.back(); res.pop_back(); cout<<el.ST<<" "<<el.ND.ST<<" "<<el.ND.ND<<"\n"; } }
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 | // Daniel Grzegorzewski // while (clock()<=69*CLOCKS_PER_SEC) #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> // #pragma GCC optimize("Ofast") // #pragma GCC target("avx,avx2,fma") #define MP make_pair #define PB push_back #define ST first #define ND second using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) //X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<PII> VII; typedef long long LL; void init_ios() { ios_base::sync_with_stdio(0); cin.tie(0); } const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count(); mt19937_64 rng(SEED); const int N = 2002; int n, m, cntRow[N], cntCol[N]; string s[N]; int row[N][26], col[N][26]; bool visC[N], visR[N]; int main() { init_ios(); // cin >> n >> m; // for (int i = 0; i < n; ++i) // cin >> s[i]; // n = m = 2000; // // cout<<"2000 2000\n"; // for (int i = 0; i < 2000; ++i) { // s[i].resize(2005); // for (int j = 0; j < 2000; ++j) { // int el = rng()%26; // s[i][j] = (char)(el+'A'); // // cout<<(char)(el+'A'); // } // // cout<<"\n"; // } cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> s[i]; for (int j = 0; j < m; ++j) { int el = (int)(s[i][j]-'A'); if (row[i][el] == 0) ++cntRow[i]; ++row[i][el]; if (col[j][el] == 0) ++cntCol[j]; col[j][el]++; } } set<pair<int, pair<char, int>>> se; for (int i = 0; i < n; ++i) { // cout<<"i = "<<i<<", "<<cntRow[i]<<"\n"; se.insert({cntRow[i], {'R', i}}); } for (int j = 0; j < m; ++j) { // cout<<"j = "<<j<<", "<<cntCol[j]<<"\n"; se.insert({cntCol[j], {'K', j}}); } vector<pair<char, pair<int, char>>> res; // cout<<"elo\n"; while (!se.empty()) { auto el = *se.begin(); se.erase(se.begin()); // cout<<el.ST<<", "<<el.ND.ST<<", "<<el.ND.ND<<"\n"; char qwe; bool bad = false; if (el.ND.ST == 'R') { visR[el.ND.ND] = true; if (cntRow[el.ND.ND] == 0) bad = true; else { for (int e = 0; e < 26; ++e) if (row[el.ND.ND][e] > 0) { qwe = (char)(e+'A'); break; } } // cout<<"qwe = "<<qwe<<"\n"; for (int j = 0; j < m; ++j) { if (visC[j]) continue; if (col[j][qwe-'A'] > 0) --col[j][qwe-'A']; if (col[j][qwe-'A'] == 0) { se.erase({cntCol[j], {'K', j}}); --cntCol[j]; se.insert({cntCol[j], {'K', j}}); } } } else { visC[el.ND.ND] = true; if (col[el.ND.ND] == 0) bad = true; else { for (int e = 0; e < 26; ++e) if (col[el.ND.ND][e] > 0) { qwe = (char)(e+'A'); break; } } // cout<<"qqwe = "<<qwe<<"\n"; for (int i = 0; i < n; ++i) { if (visR[i]) continue; if (row[i][qwe-'A'] > 0) --row[i][qwe-'A']; if (row[i][qwe-'A'] == 0) { se.erase({cntRow[i], {'R', i}}); --cntRow[i]; se.insert({cntRow[i], {'R', i}}); } } } if (!bad) res.PB({el.ND.ST, {el.ND.ND+1, qwe}}); } cout<<res.size()<<"\n"; while (res.size()) { auto el = res.back(); res.pop_back(); cout<<el.ST<<" "<<el.ND.ST<<" "<<el.ND.ND<<"\n"; } } |