#include <bits/stdc++.h> using namespace std; typedef long long ll; #define vc vector #define st first #define nd second #define pll pair<ll, ll> #define sz(a) (ll)a.size() #define all(a) a.begin(), a.end() #define pu push #define pub push_back #define pob pop_back #define em emplace #define emb emplace_back const ll INF = 1e18; struct Tree { ll base; vc<ll> t; Tree(ll n) { base = 1; while (base < n) base *= 2; t.assign(2 * base, INF); } void upd(ll i, ll x) { i += base; t[i] = x; i /= 2; while (i >= 1) { t[i] = min(t[2 * i], t[2 * i + 1]); i /= 2; } } void add(ll i, ll x) { i += base; t[i] += x; i /= 2; while (i >= 1) { t[i] = min(t[2 * i], t[2 * i + 1]); i /= 2; } } ll query(ll l, ll r) { ll ret = INF; l += base - 1; r += base + 1; while (l + 1 < r) { if (l % 2 == 0) ret = min(ret, t[l + 1]); if (r % 2 == 1) ret = min(ret, t[r - 1]); l /= 2; r /= 2; } return ret; } }; ll n, m, N, f; vc<vc<ll>> a, prf; ll fct(ll x) { ll ret = 1; for (ll i = 1; i <= x; i++) ret *= i; return ret; } void input() { cin >> m >> n; f = fct(m); N = f * n; a.assign(m, vc<ll>(N, 0)); for (ll i = 0; i < m; i++) { string str; cin >> str; for (ll j = 0; j < n; j++) { if (str[j] == '.') continue; for (ll k = j * f; k < (j + 1) * f; k++) a[i][k] = 1; } } prf.assign(m, vc<ll>(N + 1, 0)); for (ll i = 1; i <= N; i++) for (ll j = 0; j < m; j++) prf[j][i] = prf[j][i - 1] + a[j][i - 1]; } bool check(ll s) { vc<ll> p(m); iota(all(p), 0); do { Tree tree(N); for (ll i = 0; i < N; i++) { ll cnt = 0; for (ll j = 0; j < m; j++) if (a[j][i] == 0) cnt++; tree.upd(i, cnt - 1); } if (tree.t[1] < 0) continue; bool good = true; ll j = 0; for (ll i : p) { bool found = false; while (j + s <= N) { if (tree.query(j, j + s - 1) <= 0 or prf[i][j + s] - prf[i][j] > 0) { j++; continue; } found = true; for (ll k = j; k < j + s; k++) tree.add(k, -1); break; } if (not found) { good = false; break; } } if (good) return true; } while (next_permutation(all(p))); return false; } ll binsearch() { ll l = -1, r = N; while (l < r) { ll s = (l + r + 1) / 2; if (check(s)) l = s; else r = s - 1; } return l; } void output(ll x) { if (x == -1) { cout << "-1\n"; return; } ll g = gcd(x, f); x /= g; f /= g; cout << x << "/" << f << "\n"; } void program() { input(); output(binsearch()); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ll T = 1; while (T--) program(); 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #define vc vector #define st first #define nd second #define pll pair<ll, ll> #define sz(a) (ll)a.size() #define all(a) a.begin(), a.end() #define pu push #define pub push_back #define pob pop_back #define em emplace #define emb emplace_back const ll INF = 1e18; struct Tree { ll base; vc<ll> t; Tree(ll n) { base = 1; while (base < n) base *= 2; t.assign(2 * base, INF); } void upd(ll i, ll x) { i += base; t[i] = x; i /= 2; while (i >= 1) { t[i] = min(t[2 * i], t[2 * i + 1]); i /= 2; } } void add(ll i, ll x) { i += base; t[i] += x; i /= 2; while (i >= 1) { t[i] = min(t[2 * i], t[2 * i + 1]); i /= 2; } } ll query(ll l, ll r) { ll ret = INF; l += base - 1; r += base + 1; while (l + 1 < r) { if (l % 2 == 0) ret = min(ret, t[l + 1]); if (r % 2 == 1) ret = min(ret, t[r - 1]); l /= 2; r /= 2; } return ret; } }; ll n, m, N, f; vc<vc<ll>> a, prf; ll fct(ll x) { ll ret = 1; for (ll i = 1; i <= x; i++) ret *= i; return ret; } void input() { cin >> m >> n; f = fct(m); N = f * n; a.assign(m, vc<ll>(N, 0)); for (ll i = 0; i < m; i++) { string str; cin >> str; for (ll j = 0; j < n; j++) { if (str[j] == '.') continue; for (ll k = j * f; k < (j + 1) * f; k++) a[i][k] = 1; } } prf.assign(m, vc<ll>(N + 1, 0)); for (ll i = 1; i <= N; i++) for (ll j = 0; j < m; j++) prf[j][i] = prf[j][i - 1] + a[j][i - 1]; } bool check(ll s) { vc<ll> p(m); iota(all(p), 0); do { Tree tree(N); for (ll i = 0; i < N; i++) { ll cnt = 0; for (ll j = 0; j < m; j++) if (a[j][i] == 0) cnt++; tree.upd(i, cnt - 1); } if (tree.t[1] < 0) continue; bool good = true; ll j = 0; for (ll i : p) { bool found = false; while (j + s <= N) { if (tree.query(j, j + s - 1) <= 0 or prf[i][j + s] - prf[i][j] > 0) { j++; continue; } found = true; for (ll k = j; k < j + s; k++) tree.add(k, -1); break; } if (not found) { good = false; break; } } if (good) return true; } while (next_permutation(all(p))); return false; } ll binsearch() { ll l = -1, r = N; while (l < r) { ll s = (l + r + 1) / 2; if (check(s)) l = s; else r = s - 1; } return l; } void output(ll x) { if (x == -1) { cout << "-1\n"; return; } ll g = gcd(x, f); x /= g; f /= g; cout << x << "/" << f << "\n"; } void program() { input(); output(binsearch()); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ll T = 1; while (T--) program(); return 0; } |