#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;
int wx[4] = {-1, 0, 1, 0};
int wy[4] = {0, 1, 0, -1};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<string> a(n);
for(int i = 0; i < n; ++i){
cin >> a[i];
}
vector<vector<int>> col(n, vector<int>(m, -1));
int cn = 0;
auto dfs = [&](auto&&self, int x, int y)->void{
col[x][y] = cn;
for(int i = 0; i < 4; ++i){
int nx = x + wx[i], ny = y + wy[i];
if(nx >= 0 && nx < n && ny >= 0 && ny < m && a[nx][ny] == '#' && col[nx][ny] == -1){
self(self, nx, ny);
}
}
};
for(int i = n - 1; i >= 0; --i){
for(int j = 0; j < m; ++j){
if(a[i][j] == '.' || col[i][j] != -1){
continue;
}
dfs(dfs, i, j);
++cn;
}
}
vector<vector<array<int, 2>>> way(cn);
vector<int> mn(cn, (int)1e9);
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
if(col[i][j] != -1){
mn[col[i][j]] = min(mn[col[i][j]], n - i - 1);
}
}
}
for(int j = 0; j < m; ++j){
int lst = n;
for(int i = n - 1; i >= 0; --i){
if(col[i][j] == -1) continue;
if(lst != n && col[i][j] != col[lst][j]){
way[col[lst][j]].push_back({col[i][j], mn[col[i][j]] - mn[col[lst][j]] - (lst - i - 1)});
// cout << lst << ' ' << i << ' ' << mn[col[i][j]] - mn[col[lst][j]] - (lst - i - 1) << endl;
}
lst = i;
}
}
vector<int> dist(cn, 0), inq(cn, 1);
queue<int> que;
for(int i = 0; i < cn; ++i){
que.push(i);
}
while(!que.empty()){
int now = que.front();
que.pop();
inq[now] = 0;
// cout << now << ' ' << dist[now] << endl;
for(auto&[nxt, dis]:way[now]){
// cout << dist[now] << ' ' << nxt << ' ' << dist[nxt] << endl;
if(dist[now] + dis > dist[nxt]){
dist[nxt] = dist[now] + dis;
if(!inq[nxt]){
inq[nxt] = 1;
que.push(nxt);
}
}
}
}
// for(int i = 0; i < cn; ++i){
// cout << dist[i] << endl;
// }
vector<vector<char>> ans(n, vector<char>(m, '.'));
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
if(col[i][j] == -1){
continue;
}
ans[n - dist[col[i][j]] - 1 - (n - mn[col[i][j]] - 1 - i)][j] = '#';
}
}
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
cout << ans[i][j];
}
cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
236 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
137 ms |
81084 KB |
Output is correct |
2 |
Correct |
150 ms |
97728 KB |
Output is correct |
3 |
Correct |
135 ms |
74784 KB |
Output is correct |
4 |
Correct |
128 ms |
78308 KB |
Output is correct |
5 |
Correct |
159 ms |
112900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
236 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
137 ms |
81084 KB |
Output is correct |
25 |
Correct |
150 ms |
97728 KB |
Output is correct |
26 |
Correct |
135 ms |
74784 KB |
Output is correct |
27 |
Correct |
128 ms |
78308 KB |
Output is correct |
28 |
Correct |
159 ms |
112900 KB |
Output is correct |
29 |
Correct |
45 ms |
25164 KB |
Output is correct |
30 |
Correct |
42 ms |
21864 KB |
Output is correct |
31 |
Correct |
44 ms |
30796 KB |
Output is correct |
32 |
Correct |
23 ms |
12260 KB |
Output is correct |
33 |
Correct |
56 ms |
31720 KB |
Output is correct |
34 |
Correct |
53 ms |
24180 KB |
Output is correct |
35 |
Correct |
48 ms |
20252 KB |
Output is correct |
36 |
Correct |
39 ms |
16348 KB |
Output is correct |
37 |
Correct |
43 ms |
18252 KB |
Output is correct |
38 |
Correct |
44 ms |
22124 KB |
Output is correct |
39 |
Correct |
30 ms |
12600 KB |
Output is correct |
40 |
Correct |
39 ms |
13244 KB |
Output is correct |
41 |
Correct |
48 ms |
17632 KB |
Output is correct |
42 |
Correct |
57 ms |
28628 KB |
Output is correct |
43 |
Correct |
41 ms |
12748 KB |
Output is correct |
44 |
Correct |
40 ms |
13504 KB |
Output is correct |
45 |
Correct |
27 ms |
12392 KB |
Output is correct |
46 |
Correct |
45 ms |
15904 KB |
Output is correct |
47 |
Correct |
39 ms |
17744 KB |
Output is correct |
48 |
Correct |
42 ms |
14240 KB |
Output is correct |
49 |
Correct |
38 ms |
12772 KB |
Output is correct |
50 |
Correct |
72 ms |
52504 KB |
Output is correct |
51 |
Correct |
64 ms |
24052 KB |
Output is correct |
52 |
Correct |
89 ms |
26192 KB |
Output is correct |
53 |
Correct |
58 ms |
27320 KB |
Output is correct |
54 |
Correct |
77 ms |
38932 KB |
Output is correct |
55 |
Correct |
95 ms |
30496 KB |
Output is correct |
56 |
Correct |
80 ms |
29856 KB |
Output is correct |
57 |
Correct |
41 ms |
13840 KB |
Output is correct |
58 |
Correct |
59 ms |
13580 KB |
Output is correct |
59 |
Correct |
71 ms |
29132 KB |
Output is correct |
60 |
Correct |
47 ms |
12604 KB |
Output is correct |
61 |
Correct |
30 ms |
12272 KB |
Output is correct |
62 |
Correct |
34 ms |
13592 KB |
Output is correct |
63 |
Correct |
103 ms |
71940 KB |
Output is correct |
64 |
Correct |
83 ms |
52800 KB |
Output is correct |
65 |
Correct |
98 ms |
59300 KB |
Output is correct |
66 |
Correct |
107 ms |
54376 KB |
Output is correct |
67 |
Correct |
59 ms |
33964 KB |
Output is correct |
68 |
Correct |
43 ms |
20716 KB |
Output is correct |
69 |
Correct |
68 ms |
42532 KB |
Output is correct |
70 |
Correct |
38 ms |
18796 KB |
Output is correct |