QOJ.ac

QOJ

时间限制: 1 s 内存限制: 512 MB 总分: 100

#12962. Omar 喜欢糖果

统计

Omar 非常喜欢吃糖果,但不幸的是,大多数糖果都不健康。因此,他的父母想出了一个给每颗糖果打分的方法,分数越高代表糖果越健康(分数是一个整数,可以是正数、零或负数)。

有一天,他随父母去买糖果,发现了一家奇怪的商店,所有的糖果都存储在一个 $N$ 行 $M$ 列的二维网格中。行从上到下编号为 $1$ 到 $N$,列从左到右编号为 $1$ 到 $M$,每个单元格包含一颗糖果。

他们还注意到,任何一颗糖果(第一行的除外)都比它正上方的糖果更健康,任何一颗糖果(第一列的除外)都比它左侧的糖果更健康(“更健康”意味着如上所述分数更高)。

这家店还有一件奇怪的事情:要购买糖果,你必须选择一个糖果网格的子矩形,并购买该子矩形内的所有糖果。

Omar 的父母想要选择一个非空的子矩形,使其包含的糖果分数之和在所有可能的子矩形中最大。

例如,考虑示例输入中的网格。他们可以选择的一些可能的子矩形包括 $[-2, -1, 2, 3]$、$[-4, -2, -1]$ 或 $[2, 3, 4, 5]$。最后一个子矩形的得分之和最大,为 $14$。他们不能选择以下糖果列表 $[1, 2, 3, 4, 5]$ 或 $[-2, -1, 2]$(因为这些列表在给定的网格中不构成子矩形)。

你能通过编写程序来帮助他们找到给定网格中得分之和最大的非空子矩形吗?

输入格式

你的程序将在一个或多个测试用例上进行测试。输入的第一行是一个整数 $T$,表示测试用例的数量($1 \le T \le 100$)。接下来是各个测试用例,每个测试用例的第一行包含两个由空格分隔的整数 $N$ 和 $M$($1 \le N, M \le 1,000$),表示糖果网格的尺寸,随后是 $N$ 行,每行包含 $M$ 个由空格分隔的整数,表示该行糖果的得分。给定的网格表示满足上述条件,且网格中的每个整数不小于 $-2,000$ 且不大于 $2,000$。

输出格式

对于每个测试用例,输出一行,包含一个整数,表示从非空子矩形中可以获得的最大得分之和。

样例

输入 1

1
3 3
-4 -2 -1
-3 2 3
1 4 5

输出 1

14

Figure 1. 糖果网格示例

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.