D - 警備の配置 / Guard Placement 解説 by admin 印刷ページ
gpt-6-astra-mediumOverview
We want to find the number of squares visible when standing on each empty square and output the maximum value. By processing the “contiguous segments of empty squares” in the horizontal and vertical directions collectively, we can compute the answer in \(O(HW)\) time overall.
Analysis
The visible range is a horizontal and vertical contiguous segment
The horizontally visible range from a given empty square is the contiguous segment of empty squares containing that square, bounded by obstacles or the edges of the grid. The same applies vertically.
Let \(L_{i,j}\) be the length of the horizontal segment containing square \((i,j)\), and \(V_{i,j}\) be the length of the vertical segment containing it. The number of visible squares is:
\(L_{i,j}+V_{i,j}-1\)
Since the square you are standing on is included in both the horizontal and vertical segments, we subtract \(1\) to avoid double-counting.
For example, consider standing on the center square of the following grid:
#.#
...
#.#
From this square, \(3\) squares are visible horizontally and \(3\) squares are visible vertically, so the total is \(3+3-1=5\) squares.
Searching from each square every time is too slow
A naive approach would be to start from every empty square, move in all four directions (up, down, left, right), and count squares until hitting an obstacle or the edge of the grid.
However, since this takes up to \(O(H+W)\) time per square, the total time complexity would be \(O(HW(H+W))\). For instance, if \(H=1,\ W=10^5\) and all squares are empty, the same row would be traversed repeatedly, which will not pass within the time limit.
Squares in the same segment share the segment length
Suppose a row looks like this:
..#....
From either of the \(2\) squares on the left, \(2\) squares are visible horizontally. From any of the \(4\) squares on the right, \(4\) squares are visible horizontally.
In other words, once we find a contiguous segment of empty squares, we can assign its length to all squares within that segment at once.
By performing this process both horizontally and vertically, we eliminate the need to search the same range multiple times.
Algorithm
In the implementation, we use \(0\)-based indexing for row and column numbers.
1. Record horizontal segment lengths
Scan each row from left to right.
- If it is an obstacle
#, move to the next square. - If it is an empty square
., find the contiguous segment of empty squares. - Letting
startbe the start of the segment andjbe the index right after the end, the length of the segment isj - start. - Record this length for each square in the segment.
In the code, the horizontal segment length of square \((i,j)\) is stored in horizontal[i * W + j].
2. Update the answer while examining vertical segments
Initialize the answer answer to \(-1\), and scan each column from top to bottom.
Similar to the horizontal case, find each contiguous segment of empty squares and let its length be length.
For each square \((k,j)\) in that segment, compute the number of visible squares:
horizontal[k * W + j] + length - 1
and update answer with the maximum value.
Since the vertical segment length can be used immediately to calculate the answer upon finding it, there is no need to store it in an array.
3. Output the answer
Because we compute the correct number of visible squares from the horizontal and vertical segment lengths for all empty squares, the maximum value among them is the answer.
If no empty squares exist, no update will ever occur, and the initial value of \(-1\) will be output directly.
Complexity
- Time complexity: \(O(HW)\)
- Space complexity: \(O(HW)\)
In both the horizontal and vertical directions, finding segments and processing the squares inside them visits each square only a constant number of times. Therefore, even though the loops are nested, the overall time complexity is \(O(HW)\).
Storing the grid and the horizontal segment lengths requires \(O(HW)\) memory, so the space complexity is \(O(HW)\).
Implementation Notes
Deducting the square itself to avoid double-counting
Always subtract \(1\) after adding the horizontal and vertical segment lengths.Treat segments as half-open intervals
For example, in the horizontal direction, treat a segment as[start, j). The length isj - start, and the traversal condition inside the segment isk < j.Mapping 2D coordinates to a 1D array
A \(0\)-indexed square \((i,j)\) corresponds to the indexi * W + j.Initialize the answer to \(-1\)
This allows handling the case where there are no empty squares without any special conditional branching.Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int H, W;
cin >> H >> W;
vector<string> S(H);
for (auto& row : S) cin >> row;
vector<int> horizontal(H * W, 0);
for (int i = 0; i < H; ++i) {
int j = 0;
while (j < W) {
if (S[i][j] == '#') {
++j;
continue;
}
int start = j;
while (j < W && S[i][j] == '.') ++j;
int length = j - start;
for (int k = start; k < j; ++k) {
horizontal[i * W + k] = length;
}
}
}
int answer = -1;
for (int j = 0; j < W; ++j) {
int i = 0;
while (i < H) {
if (S[i][j] == '#') {
++i;
continue;
}
int start = i;
while (i < H && S[i][j] == '.') ++i;
int length = i - start;
for (int k = start; k < i; ++k) {
answer = max(answer, horizontal[k * W + j] + length - 1);
}
}
}
cout << answer << '\n';
return 0;
}
This editorial was generated by gpt-6-astra-medium.
投稿日時:
最終更新: