CF2252C.Risky Tower

普及/提高-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are playing a 2D Jenga game represented by a grid with nn rows and mm columns. The 11-st row is the top level of the tower, and the nn-th row is the bottom level.

Each piece at row ii and column jj has a destabilization factor ai,ja_{i,j}. Additionally, each row ii has an initial stability index of viv_i.

When you remove a piece, it is completely discarded from the game. Removing a piece from row ii damages all levels at and above it. Specifically, for every row kk such that 1≤k≤i1 \le k \le i, its stability index is decreased by ai,ja_{i,j}.

The tower collapses if either of the following conditions is met:

  • The stability index of any level drops to 00 or less.
  • Any level is left with exactly 00 pieces (even if it is the topmost level).

Find the minimum number of pieces you must remove such that the tower collapses.

你正在玩一个由 nn 行 mm 列网格表示的二维叠叠乐(Jenga)游戏。第 11 行是塔的最顶层,第 nn 行是塔的最底层。

位于第 ii 行、第 jj 列的每一块积木具有一个失稳因子 ai,ja_{i,j}。此外,每一行 ii 具有一个初始稳定性指数 viv_i。

当你移除一块积木时,它将被完全从游戏中丢弃。移除第 ii 行的一块积木会对第 ii 行及所有其上方的层级造成损害。具体而言,对每个满足 1≤k≤i1 \le k \le i 的行 kk,其稳定性指数均减少 ai,ja_{i,j}。

当满足以下任一条件时,塔即倒塌:

  • 任一层级的稳定性指数降至 00 或更低;
  • 任一层级剩余积木数量恰好为 00(即使该层是最顶层)。

求使塔倒塌所需移除的最少积木数量。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n,m≤1061 \le n, m \le 10^6) — the number of rows and columns of the tower.

The second line contains nn integers v1,v2,…,vnv_1, v_2, \ldots, v_n (1≤vi≤1091 \le v_i \le 10^9) — the initial stability indices of each level from top to bottom.

Each of the next nn lines contains mm integers. The jj-th integer on the ii-th line is ai,ja_{i, j} (1≤ai,j≤1091 \le a_{i, j} \le 10^9) — the destabilization factor of the piece at row ii and column jj.

It is guaranteed that the sum of n⋅mn \cdot m over all test cases does not exceed 10610^6.

每个测试包含多个测试用例。第一行包含测试用例数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n,m≤1061 \le n, m \le 10^6)—— 分别表示塔的行数与列数。

第二行包含 nn 个整数 v1,v2,…,vnv_1, v_2, \ldots, v_n(1≤vi≤1091 \le v_i \le 10^9)—— 表示从顶层到底层每一层的初始稳定性指数。

接下来的 nn 行中,每行包含 mm 个整数。第 ii 行的第 jj 个整数为 ai,ja_{i, j}(1≤ai,j≤1091 \le a_{i, j} \le 10^9)—— 表示第 ii 行第 jj 列方块的失稳因子。

保证所有测试用例中 n⋅mn \cdot m 的总和不超过 10610^6。

输出格式

For each test case, output a single integer — the minimum number of pieces that must be removed to collapse the tower.

对于每个测试用例,输出一个整数——即为使塔倒塌所需移除的最少方块数量。

输入输出样例

  • 输入#1

    2
    2 3
    10 20
    2 2 2
    5 5 5
    3 1
    100 100 100
    1
    2
    3

    输出#1

    2
    1

说明/提示

In the first testcase, we have a 2×32 \times 3 tower. The initial stabilities are v1=10v_1 = 10 and v2=20v_2 = 20. The top level (i=1i=1) has pieces with destabilization factors 2,2,22, 2, 2. The bottom level (i=2i=2) has pieces 5,5,55, 5, 5.

If we remove two pieces from the bottom level, it inflicts 5+5=105 + 5 = 10 damage to level 22, and 5+5=105 + 5 = 10 damage to level 11. The remaining stability of level 11 becomes 10−10=010 - 10 = 0. Since its stability dropped to ≤0\le 0, the tower collapses. Thus, the minimum number of pieces we must remove is 22.

In the second testcase, m=1m = 1. The rule states that the tower collapses if any level is left with exactly 00 pieces. Since every level initially has 11 piece, removing any 11 piece from the tower will instantly empty a level and cause a collapse. Therefore, the minimum number of pieces to remove is 11.

在第一个测试用例中,我们有一个 2×32 \times 3 的塔。初始稳定性分别为 v1=10v_1 = 10 和 v2=20v_2 = 20。顶层(i=1i=1)的方块具有失稳因子 2,2,22, 2, 2;底层(i=2i=2)的方块失稳因子为 5,5,55, 5, 5。

如果我们从底层移除两个方块,则会对第 22 层造成 5+5=105 + 5 = 10 点伤害,同时也会对第 11 层造成 5+5=105 + 5 = 10 点伤害。第 11 层剩余的稳定性变为 10−10=010 - 10 = 0。由于其稳定性已降至 ≤0\le 0,塔将倒塌。因此,我们必须移除的最少方块数为 22。

在第二个测试用例中,m=1m = 1。规则规定:若任一层恰好剩下 00 个方块,则塔将倒塌。由于每一层初始均有 11 个方块,因此只要从塔中移除任意 11 个方块,就会立即清空某一层并导致倒塌。故需移除的最少方块数为 11。

输入解题思路,AI测评打分。不知道怎么写?

首页