D - 警備の配置 / Guard Placement 解説 by admin 印刷ページ
Claude Opus 5 (High)Overview
This problem asks for the maximum number of empty cells that can be seen horizontally and vertically until blocked by obstacles when standing on an empty cell in an \(H \times W\) grid. For each cell, we precompute the “length of the contiguous segment of empty cells containing that cell in the horizontal direction” and the “length of the contiguous segment of empty cells in the vertical direction”, and output the maximum value of their sum minus \(1\) to remove the duplicate count of the cell itself.
(*Note: There was a typo in the original text in the previous sentence: it was intended to read “the horizontal segment containing that cell…”.)
Analysis
Decomposing the Count of Visible Cells
The cells visible when standing on cell \((i,j)\) are the union of:
- The contiguous empty cells extending left and right from \((i,j)\) within the same row \(i\) until hitting an obstacle
- The contiguous empty cells extending up and down from \((i,j)\) within the same column \(j\) until hitting an obstacle
The crucial point here is that:
The range reachable to the left and right from \((i,j)\) = the entire “maximal contiguous segment of empty cells containing \((i,j)\) in the horizontal direction”
Since obstacles do not appear inside the segment, regardless of where \((i,j)\) is located within that segment, the entire maximal horizontal segment is visible. In other words, all cells belonging to the same horizontal segment can see the exact same number of cells horizontally. The same applies to the vertical direction.
Therefore, if we define: - \(R(i,j)\) = the length of the maximal contiguous empty segment containing cell \((i,j)\) horizontally - \(C(i,j)\) = the length of the maximal contiguous empty segment containing cell \((i,j)\) vertically
Then the number of visible cells is:
\[R(i,j) + C(i,j) - 1\]
(where the \(-1\) accounts for cell \((i,j)\) itself being counted in both the horizontal and vertical directions).
Concrete Example:
.#.
...
.#.
For the center cell \((2,2)\), the horizontal segment has length 3 (\(R=3\)), and the vertical segment also has length 3 (\(C=3\)), yielding \(3+3-1=5\). Indeed, 5 cells forming a cross shape are visible.
What About a Naive Approach?
If we directly scan in all 4 directions for each cell, the worst-case time complexity becomes \(O(HW \cdot (H+W))\). Even with \(H \times W \le 10^6\), for instance when \(H=1000\) and \(W=1000\), this takes roughly \(10^6 \times 2000 = 2 \times 10^9\) operations and will exceed the time limit (the worst case occurs when all cells are .).
Thus, using the observation above, we can “compute the length for each segment all at once”, allowing us to find \(R\) and \(C\) for all cells in \(O(HW)\) time overall.
Algorithm
- Prepare two \(H \times W\) arrays,
rowLenandcolLen(initialized to 0). - For each row \(i\), scan from left to right. Whenever a maximal contiguous segment \([j, k)\) of
.is found, write its length \(len = k - j\) torowLenfor all cells in that segment. Skip#. - For each column \(j\), scan from top to bottom in a similar manner and write the lengths of maximal vertical segments to
colLen. - Iterate through all cells, and for cells where
rowLenis greater than \(0\) (= empty cells), take the maximum value of \(rowLen + colLen - 1\). - If there are no empty cells at all, output
-1.
Since each cell in a segment is written to once per direction, the total work for both the row and column directions is \(O(HW)\) each.
Complexity
- Time Complexity: \(O(HW)\)
- Space Complexity: \(O(HW)\)
Since \(HW \le 10^6\), two int arrays (around 8 MB) easily fit within the memory limit.
Implementation Details
Flatten 2D arrays into 1D: Since \(H\) and \(W\) can each be up to \(10^5\), using
vector<vector<int>>(H, vector<int>(W))can cause significant overhead depending on memory allocation, even under the constraint \(H \times W \le 10^6\). Allocating a singlevector<int>of size \(H \cdot W\) and computing indices asi * W + jis both safer and faster. When calculating indices, cast to prevent overflow, such as(size_t)i * W + j(though \(H \cdot W \le 10^6\) fits in a standardint, it is good habit to stay on the safe side).Fast I/O: Because \(H\) can be up to \(10^5\) lines, if you use
cin, be sure to includeios::sync_with_stdio(false); cin.tie(nullptr);, or usescanf("%s")for fast input reading.Cache efficiency of column-wise scanning: Scanning column by column results in strided memory access with poor cache locality, but for \(HW \le 10^6\) this will not be an issue. If performance is a concern, an alternative row-major approach is using dynamic programming: “scan from top to bottom carrying over the previous value \(+ 1\)”, followed by a pass from the bottom to propagate values.
Checking for empty cells: You can check whether a cell is empty with
rowLen[p] > 0(since an empty cell must belong to a segment of length at least 1). Of course, directly checkingS[i][j] == '.'is also fine.Don’t forget the \(-1\) case: When all cells are
#, the answer should be-1. Initializing the maximum value to-1handles this naturally.Data type of the answer: The maximum value is \(R + C - 1 \le H + W - 1 < 2 \times 10^5\), which fits within a standard
int, but usinglong longdoes no harm either.Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
int H, W;
scanf("%d %d", &H, &W);
vector<string> S(H);
{
vector<char> buf(W + 10);
for(int i = 0; i < H; i++){
scanf("%s", buf.data());
S[i].assign(buf.data(), W);
}
}
// row[i][j]: number of empty cells in the maximal horizontal segment containing (i,j)
vector<int> rowLen((size_t)H * W, 0), colLen((size_t)H * W, 0);
for(int i = 0; i < H; i++){
int j = 0;
const char* s = S[i].data();
while(j < W){
if(s[j] == '#'){ j++; continue; }
int k = j;
while(k < W && s[k] == '.') k++;
int len = k - j;
for(int t = j; t < k; t++) rowLen[(size_t)i * W + t] = len;
j = k;
}
}
for(int j = 0; j < W; j++){
int i = 0;
while(i < H){
if(S[i][j] == '#'){ i++; continue; }
int k = i;
while(k < H && S[k][j] == '.') k++;
int len = k - i;
for(int t = i; t < k; t++) colLen[(size_t)t * W + j] = len;
i = k;
}
}
long long ans = -1;
bool any = false;
for(size_t p = 0; p < (size_t)H * W; p++){
if(rowLen[p] > 0){
any = true;
long long v = (long long)rowLen[p] + colLen[p] - 1;
if(v > ans) ans = v;
}
}
if(!any) printf("-1\n");
else printf("%lld\n", ans);
return 0;
}
This editorial was generated by claude5opus-high.
投稿日時:
最終更新: