D - 警備の配置 / Guard Placement 解説 by admin 印刷ページ
Gemini 3.8 Flash (High)Overview
Given an \(H \times W\) grid, this problem asks us to find the maximum number of empty cells that can be seen vertically and horizontally from an empty cell without being blocked by obstacles.
Analysis
Counting Visible Cells
The number of visible cells from a given empty cell \((i, j)\) can be decomposed as follows: - Horizontal direction: The number of contiguous empty cells containing cell \((i, j)\) delimited by obstacles to the left and right. - Vertical direction: The number of contiguous empty cells containing cell \((i, j)\) delimited by obstacles above and below.
The total number of cells visible from cell \((i, j)\) is the sum of these two counts minus \(1\), to avoid double-counting cell \((i, j)\) itself.
Naive Approach and Its Limitations
One straightforward approach is to check, for each empty cell \((i, j)\), how many cells we can traverse in each of the \(4\) directions (up, down, left, right). However, checking a single cell in this manner takes \(O(H + W)\) time in the worst case. Performing this check for all cells would lead to an overall time complexity of \(O(HW(H + W))\). Given the constraint \(H \times W \leq 10^6\), in an extreme case such as \(H = 1, W = 10^6\), the number of operations would reach approximately \(10^{12}\), resulting in Time Limit Exceeded (TLE).
Optimization via Precomputation
Empty cells that belong to the same contiguous segment within the same row (or column) all share the exact same number of visible cells in that direction.
For example, if a row is ...#..:
- For each of the \(3\) cells ... on the left, the number of visible empty cells in that row is \(3\).
- For each of the \(2\) cells .. on the right, the number of visible empty cells in that row is \(2\).
Therefore, instead of searching in each direction from every cell, we can achieve a significant speedup by precomputing the lengths of contiguous segments of empty cells for each row and column.
Algorithm
- Precomputing row segment lengths:
Scan each row \(i\) from left to right. Find each contiguous segment \([l, r)\) of
.delimited by obstacles#, and record its length \(len = r - l\) as \(L_{\text{row}}[i][j] = len\) for all cells \((i, j)\) within the segment (\(l \leq j < r\)). - Precomputing column segment lengths:
Scan each column \(j\) from top to bottom. Similarly, find each contiguous segment \([u, d)\) of
., and record its length \(len = d - u\) as \(L_{\text{col}}[i][j] = len\) for all cells \((i, j)\) within the segment (\(u \leq i < d\)). - Calculating the answer:
Iterate through all cells \((i, j)\).
- If \(S_{i, j} =\)
., the number of visible cells is \(L_{\text{row}}[i][j] + L_{\text{col}}[i][j] - 1\). Update the maximum value with this count. - If there are no empty cells at all, output the initial value
-1.
- If \(S_{i, j} =\)
Complexity
- Time Complexity: \(O(HW)\)
- During the row-wise and column-wise precomputations, each cell is visited at most twice (once when identifying segment boundaries, and once when writing the length).
- Computing the answer also requires scanning all cells just once.
- In total, the time complexity is \(O(HW)\), which is fast enough since \(HW \leq 10^6\).
- Space Complexity: \(O(HW)\)
- \(O(HW)\) memory is used to store the input grid/strings and the precomputed results for rows and columns.
Implementation Details
Flattening the grid into a 1D array: Using a 2D
vector<vector<int>>may introduce noticeable memory allocation overhead. By flattening the grid into a 1D array of size \(H \times W\) and indexing cell \((i, j)\) asi * W + j, you can reduce execution time and save memory.Handling the case with no empty cells: By initializing the variable tracking the maximum answer to
-1, it will automatically output-1if no empty cells are found.Source Code
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int H, W;
if (!(cin >> H >> W)) return 0;
vector<string> S(H);
for (int i = 0; i < H; ++i) {
cin >> S[i];
}
vector<int> L_row(H * W, 0);
for (int i = 0; i < H; ++i) {
int l = 0;
while (l < W) {
if (S[i][l] == '#') {
l++;
continue;
}
int r = l;
while (r < W && S[i][r] == '.') {
r++;
}
int len = r - l;
for (int j = l; j < r; ++j) {
L_row[i * W + j] = len;
}
l = r;
}
}
vector<int> L_col(H * W, 0);
for (int j = 0; j < W; ++j) {
int u = 0;
while (u < H) {
if (S[u][j] == '#') {
u++;
continue;
}
int d = u;
while (d < H && S[d][j] == '.') {
d++;
}
int len = d - u;
for (int i = u; i < d; ++i) {
L_col[i * W + j] = len;
}
u = d;
}
}
int ans = -1;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
if (S[i][j] == '.') {
ans = max(ans, L_row[i * W + j] + L_col[i * W + j] - 1);
}
}
}
cout << ans << "\n";
return 0;
}
This editorial was generated by gemini-3.8-flash-high.
投稿日時:
最終更新: