-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathKMin.cpp
200 lines (164 loc) · 4.24 KB
/
KMin.cpp
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
#include <cstdio>
#include <algorithm>
#include <cstring>
//#define NDEBUG
#include <cassert>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <cstdlib>
#include <array>
#include <type_traits>
#include <queue>
#include <deque>
#include <functional>
using namespace std;
#define int64 long long
#define repeat(x) for (auto repeat_var = 0; repeat_var < x; ++repeat_var)
#define for_inc(i, x) for (auto i = 0; i < x; ++i)
#define for_dec(i, x) for (auto i = x - 1; i >= 0; --i)
#define for_inc_range(i, x, y) for (auto i = x; i <= y; ++i)
#define for_dec_range(i, x, y) for (auto i = x; i >= y; --i)
#define fill0(x) memset(x, 0, sizeof(x))
#define INT_INF 2E9L
#define ntype int
#define N_INF INT_INF
void testGen() {
freopen("biginput1.txt", "w", stdout);
fclose(stdout);
}
#define MAXN 2020
#define MAXK 2020
int nRow, nCol, nCar;
char board[MAXN][MAXN];
pair<int, int> carPos[MAXK];
int f[MAXN][MAXN];
int posLeft[MAXN][MAXN];
int posRight[MAXN][MAXN];
int ret[MAXK];
// Compute all min(ai, ..., ai + k - 1) for each i in O(n)
// Usage: KMin(k): initialize KMin algorithm with the given k
// absorb(x): absorb the next element
// getMin() return the minimum among the last absorbed k elements
template <class T> class KMin {
deque<pair<int, T>> a;
int k;
int last = 0;
function<bool(T, T)> comp;
public:
KMin(int k) : KMin(k, [](T a, T b) { return a < b; }) {}
KMin(int k, function<bool(T, T)> comp) : k(k), comp(comp) {}
void absorb(T x) {
last++;
if (a.empty()) {
a.push_back(make_pair(last, x));
} else {
while (!a.empty() && !comp(a.back().second, x)) {
a.pop_back();
}
a.push_back(make_pair(last, x));
while (!a.empty() && a.front().first <= last - k) {
a.pop_front();
}
}
}
T getMin() { return a.front().second; }
};
bool ok(int side, int r, int c) {
if (side == 1) {
return true;
}
int top = max(1, r - side + 1), bottom = top + side - 1;
if (bottom > nRow || top > r) {
return false;
}
KMin<int> maxLeft(side, [](int a, int b) { return a > b; });
KMin<int> minRight(side);
for_inc_range(row, top, bottom) {
maxLeft.absorb(posLeft[row][c] + 1);
minRight.absorb(posRight[row][c] - 1);
}
while (1) {
auto ml = maxLeft.getMin();
auto mr = minRight.getMin();
if (ml <= mr && mr - ml + 1 >= side) {
return true;
}
top++;
bottom++;
if (bottom > nRow || top > r) {
break;
}
maxLeft.absorb(posLeft[bottom][c] + 1);
minRight.absorb(posRight[bottom][c] - 1);
}
return false;
}
void recalcRow(int r) {
for_inc_range(c, 1, nCol) {
char ch = board[r - 1][c - 1];
if (ch == 'X') {
posLeft[r][c] = c;
posRight[r][c] = c;
}
}
posLeft[r][0] = 0;
posRight[r][nCol + 1] = nCol + 1;
for_inc_range(c, 1, nCol) {
char ch = board[r - 1][c - 1];
if (ch == '.') {
posLeft[r][c] = posLeft[r][c - 1];
}
}
for_dec_range(c, nCol, 1) {
char ch = board[r - 1][c - 1];
if (ch == '.') {
posRight[r][c] = posRight[r][c + 1];
}
}
}
// Sample: CF274 - E
int main() {
// testGen();
freopen("input1.txt", "r", stdin);
scanf("%d%d%d", &nRow, &nCol, &nCar);
for_inc(row, nRow) { scanf("%s", board[row]); }
for_inc_range(i, 1, nCar) {
int r, c;
scanf("%d%d", &r, &c);
carPos[i] = make_pair(r, c);
board[r - 1][c - 1] = 'X';
}
ret[nCar] = 0;
fill0(f);
for_inc_range(r, 1, nRow) {
for_inc_range(c, 1, nCol) {
char ch = board[r - 1][c - 1];
if (ch == '.') {
f[r][c] = min(f[r - 1][c], min(f[r][c - 1], f[r - 1][c - 1])) + 1;
ret[nCar] = max(ret[nCar], f[r][c]);
}
}
}
for_inc_range(r, 1, nRow) { recalcRow(r); }
for_dec_range(car, nCar - 1, 1) {
pair<int, int> pos = carPos[car + 1];
board[pos.first - 1][pos.second - 1] = '.';
recalcRow(pos.first);
int l = 1, r = min(nRow, nCol), mid = 0, best = 0;
while (l <= r) {
mid = (l + r) / 2;
if (ok(mid, pos.first, pos.second)) {
best = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
ret[car] = max(ret[car + 1], best);
}
for_inc_range(car, 1, nCar) { cout << ret[car] << endl; }
return 0;
}