#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) X.begin(), X.end()
#define sz(X) int(size(X))
#define pb push_back
#define eb emplace_back
#define st first
#define nd second
using pii = pair<int, int>; using vi = vector<int>;
using ll = long long; using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) { *(int *)0 = 0; });
#define DTP(x, y) auto operator<<(auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; }
auto operator<<(auto &o, auto a) -> decltype(all(a), o);
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
#define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", [](auto... arg_) { (( cerr << arg_ << ", " ), ...) << '\n'; }(x)
#else
#define deb(...) 0
#endif
char buf[450 << 20] alignas(16);
size_t buf_ind = sizeof buf;
template<class T> struct small {
typedef T value_type;
small(){}
template<class U> small(const U&) {}
T* allocate(size_t n) {
buf_ind -= n * sizeof(T);
buf_ind &= 0 - alignof(T);
return (T*)(buf + buf_ind);
}
void deallocate(T*, size_t) {} };
const int INF = 1e9;
template<class T>
struct RMQ {
vector<vector<T> > s;
RMQ(vector<T> a = {}) : s(1, a) {
if (!sz(a)) return;
rep(d, __lg(sz(a))) {
s.eb(sz(a) - (1 << d) * 2 + 1);
rep(j, sz(s[d + 1]))
s[d + 1][j] = min(s[d][j], s[d][j + (1 << d)]);
}
}
T get(int l, int r) {
int d = __lg(r - l + 1);
return min(s[d][l], s[d][r - (1 << d) + 1]);
}
};
template<class T>
struct RMQF {
static constexpr int B = 32; // not larger!
RMQ<T> s;
vector<uint32_t> m;
vector<T> a, c;
RMQF(vector<T> A = {}) : m(sz(A)), a(A), c(sz(A)) {
vector<T> b(sz(a) / B + 1);
uint32_t mi = 0;
rep(i, sz(a)) {
b[i / B] = (i % B ? min(b[i / B], a[i]) : a[i]);
mi <<= 1;
while (mi && a[i] < a[i - __builtin_ctz(mi)])
mi ^= (1u << __builtin_ctz(mi));
m[i] = mi ^= 1; c[i] = a[i - __lg(m[i])];
}
s = RMQ(b);
}
T get(int l, int r) {
if (r - l + 1 < B)
return a[r - __lg(m[r] & ((1u << (r - l + 1)) - 1))];
T k = min(c[r], c[l + B - 1]);
l = (l + B - 1) / B, r = r / B - 1;
if (l <= r) k = min(k, s.get(l, r));
return k;
}
};
int conv(int val, int pos) {
pos = (1 << 23) - pos;
return (val << 24) + pos;
}
pii deconv(int valpos) {
int val = valpos >> 24;
int pos = valpos & ((1 << 24) - 1);
pos = (1 << 23) - pos;
return {val, pos};
}
int n, L;
vector<string> board;
vector<vi> nexFree;
RMQF<int> rmq;
void setup(int a, int b) {
if (sz(rmq.a) != L * b) {
vector<int> irmq;
irmq.reserve(L * b);
rep(id, L) {
int fr = 0;
rep(i, n) fr += board[i][id] == '.';
rep(r, b)
irmq.pb(conv(fr, r + id * b));
}
rmq = RMQF(irmq);
}
nexFree.resize(n);
rep(i, n) nexFree[i].resize(L * b);
vi freeSoFar(n, 0), setTo(n, -1);
for (int id = L * b - 1; id >= 0; --id) {
int freeHere = 0;
rep(i, n) freeHere += board[i][id / b] == '.';
assert(freeHere);
if (freeHere == 1) {
freeSoFar.assign(n, 0);
}
else {
rep(i, n) {
if (board[i][id / b] == '.') {
++freeSoFar[i];
if (freeSoFar[i] >= a)
setTo[i] = id;
}
else {
freeSoFar[i] = 0;
}
}
}
rep(i, n)
nexFree[i][id] = setTo[i];
}
}
int getNex(int i, int S) {
if (S >= sz(nexFree[i]))
return -1;
return nexFree[i][S];
}
namespace rec {
int a;
int fix(int i, int old, const vector<pii, small<pii>> &mods) {
int modsSum = 0;
// deb(i,old,mods);
rep(idmods, sz(mods) - 1) {
int l = mods[idmods].st;
int r = mods[idmods+1].st - 1; // [l, r]
modsSum += mods[idmods].nd;
if (old > r) continue;
if (old+a-1 < l) break;
int le = max(l, old);
int ri = min(r, old+a-1);
assert(le <= ri);
auto [minVal, minPos] = deconv(rmq.get(le, ri));
minVal += modsSum;
// deb(l,r,le,ri,minVal,minPos);
assert(minVal >= 1);
if (minVal == 1) {
old = getNex(i, minPos + 1);
if (old == -1)
return old;
modsSum -= mods[idmods].nd;
--idmods;
continue;
}
}
// deb(i,old,mods);
return old;
}
vector<pii, small<pii>> addToMods(const vector<pii, small<pii>> &mods, int pos1, int val1, int pos2, int val2) {
vector<pii, small<pii>> rmods;
rmods.reserve(sz(mods) + 2);
int i = 0;
while (i < sz(mods) && mods[i].st < pos1) {
rmods.pb(mods[i]);
++i;
}
if (mods[i].st == pos1) {
rmods.eb(pos1, mods[i].nd + val1);
++i;
}
else {
rmods.eb(pos1, val1);
}
while (i < sz(mods) && mods[i].st < pos2) {
rmods.pb(mods[i]);
++i;
}
if (mods[i].st == pos2) {
rmods.eb(pos2, mods[i].nd + val2);
++i;
}
else {
rmods.eb(pos2, val2);
}
while (i < sz(mods)) {
rmods.pb(mods[i]);
++i;
}
return rmods;
}
void resort(vector<pii, small<pii>> &toFin, int i) {
while (i+1 < sz(toFin) && toFin[i].st > toFin[i+1].st) {
swap(toFin[i], toFin[i+1]);
++i;
}
}
bool isFin(const vector<pii, small<pii>> &toFin, const vector<pii, small<pii>> &mods) {
// deb(toFin, mods);
if (sz(toFin) <= 1) return true;
auto old = buf_ind;
{
auto [pos, _] = toFin[0];
vector<pii, small<pii>> here(1 + all(toFin));
vector<pii, small<pii>> hereMods = addToMods(mods, pos, -1, pos + a, +1);
auto &[ipos, i] = here[0];
ipos = fix(i, ipos, hereMods);
// deb(here);
if (ipos != -1) {
resort(here, 0);
if (isFin(here, hereMods))
return true;
}
buf_ind = old;
}
if (toFin[1].st < toFin[0].st + a) {
auto [pos, _] = toFin[1];
vector<pii, small<pii>> here(1 + all(toFin));
here[0] = toFin[0];
// deb(_, pos);
vector<pii, small<pii>> hereMods = addToMods(mods, pos, -1, pos + a, +1);
if (sz(toFin) > 2) {
auto &[ipos, i] = here[0];
ipos = fix(i, ipos, hereMods);
auto &[jpos, j] = here[1];
jpos = fix(j, jpos, hereMods);
// deb(here);
if (ipos != -1 && jpos != -1) {
resort(here, 1);
resort(here, 0);
if (isFin(here, hereMods))
return true;
}
}
else {
auto &[ipos, i] = here[0];
ipos = fix(i, ipos, hereMods);
if (ipos != -1) {
resort(here, 0);
if (isFin(here, hereMods))
return true;
}
}
buf_ind = old;
}
return false;
}
}
bool check(int a, int b) {
// deb(a, b);
setup(a, b);
// rep(i, n) deb(i, nexFree[i]);
// deb(buf_ind);
buf_ind = sizeof buf;
// deb(buf_ind);
rec::a = a;
vector<pii, small<pii>> gdje, mods;
rep(i, n) {
if (nexFree[i][0] == -1) return false;
gdje.eb(nexFree[i][0], i);
}
sort(all(gdje));
mods.eb(0, 0);
mods.eb(L * b, 0);
bool ok = rec::isFin(gdje, mods);
return ok;
}
void solve() {
rmq = RMQF<int>();
rep(id, L) {
int fr = 0;
rep(i, n) fr += board[i][id] == '.';
if (!fr) {
cout << "-1\n";
return;
}
}
// auto oke = check(5, 3);
// deb(oke);
// exit(0);
int lx = 0, rx = L;
while (rx - lx > 1) {
int midx = (lx + rx) / 2;
bool ok = check(midx, 1);
if (ok)
lx = midx;
else
rx = midx;
}
vector<pii> fracs;
for (int den = 1; den <= n; ++den)
for (int num = 0; num <= den; ++num) if (gcd(num, den) == 1)
fracs.eb(num + lx * den, den);
sort(all(fracs), [](pii a, pii b){
return a.st * b.nd < a.nd * b.st;
});
// deb(sz(fracs));
int l = 0, r = sz(fracs);
while (r - l > 1) {
int mid = (l + r) / 2;
auto [a, b] = fracs[mid];
bool ok = check(a, b);
if (ok)
l = mid;
else
r = mid;
}
auto [a, b] = fracs[l];
cout << a << '/' << b << '\n';
}
void solveInput() {
cin >> n >> L;
board.resize(n);
rep(i, n) cin >> board[i];
solve();
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cout << fixed << setprecision(10);
// n = 18;
// L = 1e5;
// board.assign(n, string(L, '.'));
// rep(i, n) rep(j, i) board[i][j] = 'X';
// solve();
// rep(val, 20) rep(pos, 3e6) {
// int x = conv(val, pos);
// auto [v, p] = deconv(x);
// assert(v == val);
// assert(p == pos);
// }
int z = 1;
// cin >> z;
while (z--)
solveInput();
cout << flush;
_Exit(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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) fwd(i, 0, n) #define all(X) X.begin(), X.end() #define sz(X) int(size(X)) #define pb push_back #define eb emplace_back #define st first #define nd second using pii = pair<int, int>; using vi = vector<int>; using ll = long long; using ld = long double; #ifdef LOC auto SS = signal(6, [](int) { *(int *)0 = 0; }); #define DTP(x, y) auto operator<<(auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; } auto operator<<(auto &o, auto a) -> decltype(all(a), o); DTP(o << a.st << ", " << a.nd, a.nd); DTP(for (auto i : a) o << i << ", ", all(a)); #define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", [](auto... arg_) { (( cerr << arg_ << ", " ), ...) << '\n'; }(x) #else #define deb(...) 0 #endif char buf[450 << 20] alignas(16); size_t buf_ind = sizeof buf; template<class T> struct small { typedef T value_type; small(){} template<class U> small(const U&) {} T* allocate(size_t n) { buf_ind -= n * sizeof(T); buf_ind &= 0 - alignof(T); return (T*)(buf + buf_ind); } void deallocate(T*, size_t) {} }; const int INF = 1e9; template<class T> struct RMQ { vector<vector<T> > s; RMQ(vector<T> a = {}) : s(1, a) { if (!sz(a)) return; rep(d, __lg(sz(a))) { s.eb(sz(a) - (1 << d) * 2 + 1); rep(j, sz(s[d + 1])) s[d + 1][j] = min(s[d][j], s[d][j + (1 << d)]); } } T get(int l, int r) { int d = __lg(r - l + 1); return min(s[d][l], s[d][r - (1 << d) + 1]); } }; template<class T> struct RMQF { static constexpr int B = 32; // not larger! RMQ<T> s; vector<uint32_t> m; vector<T> a, c; RMQF(vector<T> A = {}) : m(sz(A)), a(A), c(sz(A)) { vector<T> b(sz(a) / B + 1); uint32_t mi = 0; rep(i, sz(a)) { b[i / B] = (i % B ? min(b[i / B], a[i]) : a[i]); mi <<= 1; while (mi && a[i] < a[i - __builtin_ctz(mi)]) mi ^= (1u << __builtin_ctz(mi)); m[i] = mi ^= 1; c[i] = a[i - __lg(m[i])]; } s = RMQ(b); } T get(int l, int r) { if (r - l + 1 < B) return a[r - __lg(m[r] & ((1u << (r - l + 1)) - 1))]; T k = min(c[r], c[l + B - 1]); l = (l + B - 1) / B, r = r / B - 1; if (l <= r) k = min(k, s.get(l, r)); return k; } }; int conv(int val, int pos) { pos = (1 << 23) - pos; return (val << 24) + pos; } pii deconv(int valpos) { int val = valpos >> 24; int pos = valpos & ((1 << 24) - 1); pos = (1 << 23) - pos; return {val, pos}; } int n, L; vector<string> board; vector<vi> nexFree; RMQF<int> rmq; void setup(int a, int b) { if (sz(rmq.a) != L * b) { vector<int> irmq; irmq.reserve(L * b); rep(id, L) { int fr = 0; rep(i, n) fr += board[i][id] == '.'; rep(r, b) irmq.pb(conv(fr, r + id * b)); } rmq = RMQF(irmq); } nexFree.resize(n); rep(i, n) nexFree[i].resize(L * b); vi freeSoFar(n, 0), setTo(n, -1); for (int id = L * b - 1; id >= 0; --id) { int freeHere = 0; rep(i, n) freeHere += board[i][id / b] == '.'; assert(freeHere); if (freeHere == 1) { freeSoFar.assign(n, 0); } else { rep(i, n) { if (board[i][id / b] == '.') { ++freeSoFar[i]; if (freeSoFar[i] >= a) setTo[i] = id; } else { freeSoFar[i] = 0; } } } rep(i, n) nexFree[i][id] = setTo[i]; } } int getNex(int i, int S) { if (S >= sz(nexFree[i])) return -1; return nexFree[i][S]; } namespace rec { int a; int fix(int i, int old, const vector<pii, small<pii>> &mods) { int modsSum = 0; // deb(i,old,mods); rep(idmods, sz(mods) - 1) { int l = mods[idmods].st; int r = mods[idmods+1].st - 1; // [l, r] modsSum += mods[idmods].nd; if (old > r) continue; if (old+a-1 < l) break; int le = max(l, old); int ri = min(r, old+a-1); assert(le <= ri); auto [minVal, minPos] = deconv(rmq.get(le, ri)); minVal += modsSum; // deb(l,r,le,ri,minVal,minPos); assert(minVal >= 1); if (minVal == 1) { old = getNex(i, minPos + 1); if (old == -1) return old; modsSum -= mods[idmods].nd; --idmods; continue; } } // deb(i,old,mods); return old; } vector<pii, small<pii>> addToMods(const vector<pii, small<pii>> &mods, int pos1, int val1, int pos2, int val2) { vector<pii, small<pii>> rmods; rmods.reserve(sz(mods) + 2); int i = 0; while (i < sz(mods) && mods[i].st < pos1) { rmods.pb(mods[i]); ++i; } if (mods[i].st == pos1) { rmods.eb(pos1, mods[i].nd + val1); ++i; } else { rmods.eb(pos1, val1); } while (i < sz(mods) && mods[i].st < pos2) { rmods.pb(mods[i]); ++i; } if (mods[i].st == pos2) { rmods.eb(pos2, mods[i].nd + val2); ++i; } else { rmods.eb(pos2, val2); } while (i < sz(mods)) { rmods.pb(mods[i]); ++i; } return rmods; } void resort(vector<pii, small<pii>> &toFin, int i) { while (i+1 < sz(toFin) && toFin[i].st > toFin[i+1].st) { swap(toFin[i], toFin[i+1]); ++i; } } bool isFin(const vector<pii, small<pii>> &toFin, const vector<pii, small<pii>> &mods) { // deb(toFin, mods); if (sz(toFin) <= 1) return true; auto old = buf_ind; { auto [pos, _] = toFin[0]; vector<pii, small<pii>> here(1 + all(toFin)); vector<pii, small<pii>> hereMods = addToMods(mods, pos, -1, pos + a, +1); auto &[ipos, i] = here[0]; ipos = fix(i, ipos, hereMods); // deb(here); if (ipos != -1) { resort(here, 0); if (isFin(here, hereMods)) return true; } buf_ind = old; } if (toFin[1].st < toFin[0].st + a) { auto [pos, _] = toFin[1]; vector<pii, small<pii>> here(1 + all(toFin)); here[0] = toFin[0]; // deb(_, pos); vector<pii, small<pii>> hereMods = addToMods(mods, pos, -1, pos + a, +1); if (sz(toFin) > 2) { auto &[ipos, i] = here[0]; ipos = fix(i, ipos, hereMods); auto &[jpos, j] = here[1]; jpos = fix(j, jpos, hereMods); // deb(here); if (ipos != -1 && jpos != -1) { resort(here, 1); resort(here, 0); if (isFin(here, hereMods)) return true; } } else { auto &[ipos, i] = here[0]; ipos = fix(i, ipos, hereMods); if (ipos != -1) { resort(here, 0); if (isFin(here, hereMods)) return true; } } buf_ind = old; } return false; } } bool check(int a, int b) { // deb(a, b); setup(a, b); // rep(i, n) deb(i, nexFree[i]); // deb(buf_ind); buf_ind = sizeof buf; // deb(buf_ind); rec::a = a; vector<pii, small<pii>> gdje, mods; rep(i, n) { if (nexFree[i][0] == -1) return false; gdje.eb(nexFree[i][0], i); } sort(all(gdje)); mods.eb(0, 0); mods.eb(L * b, 0); bool ok = rec::isFin(gdje, mods); return ok; } void solve() { rmq = RMQF<int>(); rep(id, L) { int fr = 0; rep(i, n) fr += board[i][id] == '.'; if (!fr) { cout << "-1\n"; return; } } // auto oke = check(5, 3); // deb(oke); // exit(0); int lx = 0, rx = L; while (rx - lx > 1) { int midx = (lx + rx) / 2; bool ok = check(midx, 1); if (ok) lx = midx; else rx = midx; } vector<pii> fracs; for (int den = 1; den <= n; ++den) for (int num = 0; num <= den; ++num) if (gcd(num, den) == 1) fracs.eb(num + lx * den, den); sort(all(fracs), [](pii a, pii b){ return a.st * b.nd < a.nd * b.st; }); // deb(sz(fracs)); int l = 0, r = sz(fracs); while (r - l > 1) { int mid = (l + r) / 2; auto [a, b] = fracs[mid]; bool ok = check(a, b); if (ok) l = mid; else r = mid; } auto [a, b] = fracs[l]; cout << a << '/' << b << '\n'; } void solveInput() { cin >> n >> L; board.resize(n); rep(i, n) cin >> board[i]; solve(); } int32_t main() { cin.tie(0)->sync_with_stdio(0); cout << fixed << setprecision(10); // n = 18; // L = 1e5; // board.assign(n, string(L, '.')); // rep(i, n) rep(j, i) board[i][j] = 'X'; // solve(); // rep(val, 20) rep(pos, 3e6) { // int x = conv(val, pos); // auto [v, p] = deconv(x); // assert(v == val); // assert(p == pos); // } int z = 1; // cin >> z; while (z--) solveInput(); cout << flush; _Exit(0); } |
English