2025 February USACO Bronze Problems/Problem 1
Contents
Problem
Note
Note: The time limit for this problem is 4s, twice the default.
Problem
Farmer John has a square canvas represented by an N by N grid of cells (2≤N≤2000, N is even). He paints the canvas according to the following steps:
- First, he divides the canvas into four equal quadrants, separated by horizontal and vertical lines through the center of the canvas.
- Next, he paints a lovely painting in the top-right quadrant of the canvas. Each cell of the top-right quadrant will either be painted (represented by '#') or unpainted (represented by '.').
- Finally, since he is so proud of his painting, he reflects it across the previously mentioned horizontal and vertical lines into the other quadrants of the canvas.
For example, suppose N=8 and FJ painted the following painting in the top-right quadrant in step 2:
.#.. .#.. .##. ....
Then after reflecting across the horizontal and vertical lines into the other quadrants in step 3, the canvas would look as follows:
..#..#.. ..#..#.. .##..##. ........ ........ .##..##. ..#..#.. ..#..#..
However, while FJ was sleeping, Bessie broke into his barn and stole his precious canvas. She totally vandalized the canvas—removing some painted cells and adding more painted cells! Before FJ woke up, she returned the canvas to FJ.
FJ would like to modify his canvas so that it once again satisfies the reflective condition: that is, it is the result of reflecting the top-right quadrant into each of the other quadrants. Since he only has a limited number of resources, he would like to achieve this in as few operations as possible, where a single operation consists of either painting a cell or removing paint from a cell.
You are given the canvas after Bessie's vandalism, as well as a sequence of U (0≤U≤10^5) updates to the canvas, each toggling a single cell to '.' if it is '#', or vice versa. Before any updates, and after each update, output the minimum number of operations x FJ needs to perform so that the reflective condition is satisfied.
Input Format
The first line contains integers N and U.
The next N lines each contain N characters representing the canvas after Bessie's vandalism. Every character is either '#' or '.'.
The following U lines each contain integers r and c, where 1≤r,c≤N, representing an update to the cell in the rth row from the top and cth column from the left of the canvas.
Output Format
Output U+1 lines representing x before any updates and after each update.
Sample Input
4 5 ..#. ##.# #### ..## 1 3 2 3 4 3 4 4 4 4
Sample Output
4 3 2 1 0 1
Scoring
Inputs 2-3: N≤4
Inputs 4-6: U≤10
Inputs 7-16: No additional constraints.
Solution
We need to take advantage of the fact that updates only touch one square. Because exactly one square is touched per update, only four squares can possibly be impacted, namely the four squares that map to the updated one. We can temporarily ignore the optimal painted state for those four squares, perform the update, and recompute the answer.