#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define all(x) begin(x), end(x) #define sz(x) int((x).size()) using ll = long long; using pii = pair<int, int>; using vi = vector<int>; #ifdef LOCAL auto operator<<(auto& o, auto x) -> decltype(x.first, o); auto operator<<(auto& o, auto x) -> decltype(x.end(), o) { o << "{"; for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y; return o << "}"; } auto operator<<(auto& o, auto x) -> decltype(x.first, o) { return o << "(" << x.first << ", " << x.second << ")"; } void __print(auto... x) { ((cerr << x << " "), ...) << endl; } #define debug(x...) __print("[" #x "]:", x) #else #define debug(...) 2137 #endif const int nax = 1e5 + 10; const int N = 18; int pref[N][nax]; int bnd[nax]; int n, m; int cnt[N]; mt19937 rng(10); struct frac { int a, b; bool operator<(const frac o) const { return a * o.b < o.a * b; } }; bool check(vector<int>& ind, frac t) { int num = t.a; int den = t.b; fill(cnt, cnt + n, 0); queue<pair<int, int>> q1; stack<pair<int, int>> q2; int to_sub = 0; q1.push({0, bnd[0]}); cnt[bnd[0]]++; q2.push({m * den, 0}); for(int i = m - 1; i > 0; i--) q2.push({i * den, bnd[i]}); bool ok = 1; for(int i = 0; i < n; i++) { int id = ind[i]; while(!q2.empty()) { auto [cur, cur_val] = q1.front(); int rt = cur + num; while(!q2.empty() && q2.top().first < rt) { auto [j, val] = q2.top(); q2.pop(); q1.push({j, val + to_sub}); cnt[val]++; } if(q2.empty()) break; if(cnt[0] || pref[id][(rt + den - 1) / den] - pref[id][cur / den] > 0) { q1.pop(); cnt[cur_val - to_sub]--; if(q1.empty()) { auto [j, val] = q2.top(); q2.pop(); q1.push({j, val + to_sub}); cnt[val]++; } } else break; } if(q2.empty()) return 0; for(int j = 0; j < n - 1; j++) cnt[j] = cnt[j + 1]; cnt[n - 1] = 0; int cur = q1.front().first; int rt = cur + num; if(q2.top().first != rt) q2.push({rt, bnd[rt / den]}); to_sub++; } return 1; } void solve() { cin >> n >> m; vector<string> v(n); for(int i = 0; i < n; i++) cin >> v[i]; shuffle(all(v), rng); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { bnd[j] += v[i][j] == 'X'; pref[i][j + 1] = pref[i][j] + (v[i][j] == 'X'); } } for(int i = 0; i < m; i++) { bnd[i] = n - 1 - bnd[i]; if(bnd[i] < 0) { cout << -1 << endl; return; } } frac ans{0, 1}; vector<int> ind(n); iota(all(ind), 0); do { for(int mult = n; mult >= (n + 1) / 2; mult--) { if(mult == n) { int l = ans.a * mult / ans.b, r = mult * m + 1; while(l + 1 < r) { int mid = (l + r) >> 1; if(check(ind, {mid, mult})) l = mid; else r = mid; } ans = max(ans, {l, mult}); } else { int l = (ans.a * mult + ans.b - 1) / ans.b; if(check(ind, {l, mult})) { ans = max(ans, {l, mult}); } } } } while(next_permutation(all(ind))); auto [num, den] = ans; int g = gcd(num, den); num /= g; den /= g; cout << num << "/" << den << endl; } int main() { cin.tie(0)->sync_with_stdio(0); solve(); }
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 | #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define all(x) begin(x), end(x) #define sz(x) int((x).size()) using ll = long long; using pii = pair<int, int>; using vi = vector<int>; #ifdef LOCAL auto operator<<(auto& o, auto x) -> decltype(x.first, o); auto operator<<(auto& o, auto x) -> decltype(x.end(), o) { o << "{"; for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y; return o << "}"; } auto operator<<(auto& o, auto x) -> decltype(x.first, o) { return o << "(" << x.first << ", " << x.second << ")"; } void __print(auto... x) { ((cerr << x << " "), ...) << endl; } #define debug(x...) __print("[" #x "]:", x) #else #define debug(...) 2137 #endif const int nax = 1e5 + 10; const int N = 18; int pref[N][nax]; int bnd[nax]; int n, m; int cnt[N]; mt19937 rng(10); struct frac { int a, b; bool operator<(const frac o) const { return a * o.b < o.a * b; } }; bool check(vector<int>& ind, frac t) { int num = t.a; int den = t.b; fill(cnt, cnt + n, 0); queue<pair<int, int>> q1; stack<pair<int, int>> q2; int to_sub = 0; q1.push({0, bnd[0]}); cnt[bnd[0]]++; q2.push({m * den, 0}); for(int i = m - 1; i > 0; i--) q2.push({i * den, bnd[i]}); bool ok = 1; for(int i = 0; i < n; i++) { int id = ind[i]; while(!q2.empty()) { auto [cur, cur_val] = q1.front(); int rt = cur + num; while(!q2.empty() && q2.top().first < rt) { auto [j, val] = q2.top(); q2.pop(); q1.push({j, val + to_sub}); cnt[val]++; } if(q2.empty()) break; if(cnt[0] || pref[id][(rt + den - 1) / den] - pref[id][cur / den] > 0) { q1.pop(); cnt[cur_val - to_sub]--; if(q1.empty()) { auto [j, val] = q2.top(); q2.pop(); q1.push({j, val + to_sub}); cnt[val]++; } } else break; } if(q2.empty()) return 0; for(int j = 0; j < n - 1; j++) cnt[j] = cnt[j + 1]; cnt[n - 1] = 0; int cur = q1.front().first; int rt = cur + num; if(q2.top().first != rt) q2.push({rt, bnd[rt / den]}); to_sub++; } return 1; } void solve() { cin >> n >> m; vector<string> v(n); for(int i = 0; i < n; i++) cin >> v[i]; shuffle(all(v), rng); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { bnd[j] += v[i][j] == 'X'; pref[i][j + 1] = pref[i][j] + (v[i][j] == 'X'); } } for(int i = 0; i < m; i++) { bnd[i] = n - 1 - bnd[i]; if(bnd[i] < 0) { cout << -1 << endl; return; } } frac ans{0, 1}; vector<int> ind(n); iota(all(ind), 0); do { for(int mult = n; mult >= (n + 1) / 2; mult--) { if(mult == n) { int l = ans.a * mult / ans.b, r = mult * m + 1; while(l + 1 < r) { int mid = (l + r) >> 1; if(check(ind, {mid, mult})) l = mid; else r = mid; } ans = max(ans, {l, mult}); } else { int l = (ans.a * mult + ans.b - 1) / ans.b; if(check(ind, {l, mult})) { ans = max(ans, {l, mult}); } } } } while(next_permutation(all(ind))); auto [num, den] = ans; int g = gcd(num, den); num /= g; den /= g; cout << num << "/" << den << endl; } int main() { cin.tie(0)->sync_with_stdio(0); solve(); } |