E - Rectangle Coloring Editorial by evima
Focusing on cell \((1,1)\), we can see that we need to perform at least one operation choosing elements from \(L\) and \(U\).
Similar arguments for cells \((1,N),(N,1),(N,N)\) show that we need to choose two elements each from \(U,D,L,R\).
Let the chosen elements be \(U_{u1},U_{u2}, D_{d1},D_{d2},L_{l1},L_{l2},R_{r1},R_{r2}\). \((u1<u2,d1<d2,l1<l2,r1<r2)\)
First, we can assume that operations are performed by choosing \(U_{u2}\) and \(L_{l2}\). (The same applies to UR, RD, DL.) This is because when this is not the case, we can swap operation targets without loss.
Considering the union of the four regions to be colored, the cases where we cannot cover everything are limited to when \(u2+1<d1\) and \(r2+1<l1\), or when \(d2+1<u1\) and \(l2+1<r1\). In all other cases, we can cover everything.
Consider the case where four operations cannot cover everything, and we perform five operations.
Using similar arguments about operation swapping, we find that we don’t need to perform operations pairing \(U\) and \(L\) twice or more, and we don’t need to perform operations pairing \(U\) elements with each other.
From the above argument, for the fifth operation, we only need to consider pairing \(U\) with \(D\), or pairing \(L\) with \(R\).
We prove that pairing \(U\) and \(D\) can cover everything. (The proof for pairing \(R\) and \(L\) is similar.)
Re-index the chosen elements as \(U_{u1},U_{u2},U_{u3}\) for upper elements and \(D_{d1},D_{d2},D_{d3}\) for lower elements.
Then, performing operations with the combinations \((U_{u1},D_{d3}), (U_{u2},R_{r2}),(U_{u3},L_{l2}),(D_{d1},R_{r1}),(D_{d2},L_{l1})\) can cover the entire area.
From the above argument, we also know that six or more operations are unnecessary.
At this point, we understand the structure of the optimal solution.
Finding the minimum cost when performing five operations is easy.
For finding the minimum cost when performing four operations, various approaches are possible; since we can calculate the minimum cost for cases that satisfy/don’t satisfy \(u2+1<d1\), we can use this to calculate the minimum cost for covering the entire area with four operations.
posted:
last update: