2252C.md (2877B)
1 Source: https://codeforces.com/problemset/problem/2252/C 2 3 C. Risky Tower 4 5 time limit per test 6 7 3 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are playing a 2D Jenga game represented by a grid with $n$ rows and $m$ columns. The $1$-st row is the top level of the tower, and the $n$-th row is the bottom level. 22 23 Each piece at row $i$ and column $j$ has a destabilization factor $a_{i,j}$. Additionally, each row $i$ has an initial stability index of $v_i$. 24 25 When you remove a piece, it is completely discarded from the game. Removing a piece from row $i$ damages all levels at and above it. Specifically, for every row $k$ such that $1 \le k \le i$, its stability index is decreased by $a_{i,j}$. 26 27 The tower collapses if either of the following conditions is met: 28 29 * The stability index of any level drops to $0$ or less. 30 * Any level is left with exactly $0$ pieces (even if it is the topmost level). 31 32 Find the minimum number of pieces you must remove such that the tower collapses. 33 34 Input 35 36 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. 37 38 The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^6$) — the number of rows and columns of the tower. 39 40 The second line contains $n$ integers $v_1, v_2, \ldots, v_n$ ($1 \le v_i \le 10^9$) — the initial stability indices of each level from top to bottom. 41 42 Each of the next $n$ lines contains $m$ integers. The $j$-th integer on the $i$-th line is $a_{i, j}$ ($1 \le a_{i, j} \le 10^9$) — the destabilization factor of the piece at row $i$ and column $j$. 43 44 It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$. 45 46 Output 47 48 For each test case, output a single integer — the minimum number of pieces that must be removed to collapse the tower. 49 50 Example 51 52 Input 53 54 ``` 55 2 56 57 2 3 58 59 10 20 60 61 2 2 2 62 63 5 5 5 64 65 3 1 66 67 100 100 100 68 69 1 70 71 2 72 73 3 74 ``` 75 76 Output 77 78 ``` 79 2 80 81 1 82 ``` 83 84 Note 85 86 In the first testcase, we have a $2 \times 3$ tower. The initial stabilities are $v_1 = 10$ and $v_2 = 20$. The top level ($i=1$) has pieces with destabilization factors $2, 2, 2$. The bottom level ($i=2$) has pieces $5, 5, 5$. 87 88 If we remove two pieces from the bottom level, it inflicts $5 + 5 = 10$ damage to level $2$, and $5 + 5 = 10$ damage to level $1$. The remaining stability of level $1$ becomes $10 - 10 = 0$. Since its stability dropped to $\le 0$, the tower collapses. Thus, the minimum number of pieces we must remove is $2$. 89 90 In the second testcase, $m = 1$. The rule states that the tower collapses if any level is left with exactly $0$ pieces. Since every level initially has $1$ piece, removing any $1$ piece from the tower will instantly empty a level and cause a collapse. Therefore, the minimum number of pieces to remove is $1$.