#P9195. Clearing the Well
Clearing the Well
Clearing the Well
The game takes place in an area called the "well" which is a rectangular grid with \(N\) columns and an arbitrarily large number of rows. The cells are denoted by \((i,j)\) where \(i\) is the column number counted from the left and \(j\) is the row number counted from the bottom. Each cell may either be empty or contain a block.
Initially, in the \(i\)-th column, exactly the cells \((i,1)\), (i,2), \dots, (i,A_i)\) have blocks. It is guaranteed that the bottom-most row is not completely filled with blocks.
After the initial configuration, exactly 104 domino pieces of size 1 \(\times\) \(K\) drop one by one. When dropping a domino, the player makes a choice:
- If vertical is chosen, the player picks an integer \(x\) with \(1 \le x \le N\). The domino will drop in column \(x\) and land so that its bottom is immediately above the top block in that column (or at the bottom of the well if the column is empty).
- If horizontal is chosen, the player picks an integer \(x\) with \(1 \le x \le N-K+1\). The domino covers columns \(x, x+1, \dots, x+K-1\) and will drop so that it lands in a single row, which is exactly one row above the highest block among those columns (or at the bottom if all these columns are empty).
After a domino stops moving, the system scans the well from the bottom upward. If any row is completely filled with blocks (i.e. all \(N\) cells are occupied), then all the blocks in that row are removed and every block above that row falls down by one cell.
After the clearance the system checks: if there are no blocks left in the well, the game terminates and the player wins; otherwise the next domino is dropped.
Your task is to determine whether the player can win. If a winning strategy exists, you must output one valid sequence of moves (no more than 104 moves) that leads to victory. Otherwise, output an indicator that win is impossible.
Note on notation: All formulas must be given in LaTeX format (e.g. using \( ... \)).
inputFormat
The first line of input contains two integers \(N\) and \(K\) (1 \le N \le 1000, 1 \le K \le 1000). The second line contains N integers: \(A_1, A_2, \dots, A_N\) (0 \le A_i \le 109), where Ai denotes the initial number of blocks in column \(i\).
It is guaranteed that the bottom-most row is not completely filled with blocks.
outputFormat
If a winning strategy exists, output YES
in the first line. In the second line, output a nonnegative integer M (M \le 104) representing the number of moves in your strategy. Then output M lines each describing a move. Each move is described by two integers t and x where:
- If t = 0, the domino is placed vertically in column \(x\) (1 \le x \le N).
- If t = 1, the domino is placed horizontally covering columns \(x, x+1, \dots, x+K-1\) (1 \le x \le N-K+1).
If a winning strategy is impossible, output a single line containing NO
.
sample
3 2
0 0 0
YES
0
</p>