CF659F.Polycarp and Hay
普及+/提高
通过率:0%
时间限制:4.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The farmer Polycarp has a warehouse with hay, which can be represented as an n × m rectangular table, where n is the number of rows, and m is the number of columns in the table. Each cell of the table contains a haystack. The height in meters of the hay located in the i-th row and the j-th column is equal to an integer a__i, j and coincides with the number of cubic meters of hay in the haystack, because all cells have the size of the base 1 × 1. Polycarp has decided to tidy up in the warehouse by removing an arbitrary integer amount of cubic meters of hay from the top of each stack. You can take different amounts of hay from different haystacks. Besides, it is allowed not to touch a stack at all, or, on the contrary, to remove it completely. If a stack is completely removed, the corresponding cell becomes empty and no longer contains the stack.
Polycarp wants the following requirements to hold after the reorganization:
- the total amount of hay remaining in the warehouse must be equal to k,
- the heights of all stacks (i.e., cells containing a non-zero amount of hay) should be the same,
- the height of at least one stack must remain the same as it was,
- for the stability of the remaining structure all the stacks should form one connected region.
The two stacks are considered adjacent if they share a side in the table. The area is called connected if from any of the stack in the area you can get to any other stack in this area, moving only to adjacent stacks. In this case two adjacent stacks necessarily belong to the same area.
Help Polycarp complete this challenging task or inform that it is impossible.
农民波利卡普有一个堆放干草的仓库,该仓库可表示为一个 n×m 的矩形表格,其中 n 为行数,m 为列数。表格的每个单元格中都有一堆干草垛。位于第 i 行、第 j 列的干草垛高度(单位:米)为整数 ai,j,且该数值恰好等于该干草垛所含干草的体积(单位:立方米),因为所有单元格的底面积均为 1×1。
波利卡普决定整理仓库,即从每堆干草垛的顶部移除任意整数体积(单位:立方米)的干草。不同干草垛上移除的干草体积可以不同。此外,允许完全不触碰某堆干草垛,也允许将其完全清空;若某堆干草垛被完全移除,则对应单元格变为空,不再包含任何干草垛。
整理后,波利卡普希望满足以下要求:
- 仓库中剩余干草的总体积恰好为 k;
- 所有未被清空的干草垛(即对应单元格中干草体积非零者)的高度必须相同;
- 至少有一堆干草垛的高度需保持原样(即其最终高度等于初始高度 ai,j);
- 为保证剩余结构的稳定性,所有未被清空的干草垛必须构成一个连通区域。
若两个干草垛在表格中共享一条边(即上下左右相邻),则称它们彼此相邻。一个区域被称为连通的,当且仅当该区域中任意一堆干草垛均可仅通过向相邻干草垛移动(每次只能移至相邻堆)而到达该区域中的任意其他干草垛。此时,任意两个相邻的干草垛必然属于同一区域。
请帮助波利卡普完成这项具有挑战性的任务;若不可能实现,请告知其不可行。
输入格式
The first line of the input contains three integers n, m (1 ≤ n, m ≤ 1000) and k (1 ≤ k ≤ 1018) — the number of rows and columns of the rectangular table where heaps of hay are lain and the required total number cubic meters of hay after the reorganization.
Then n lines follow, each containing m positive integers a__i, j (1 ≤ a__i, j ≤ 109), where a__i, j is equal to the number of cubic meters of hay making the hay stack on the i-th row and j-th column of the table.
输入的第一行包含三个整数 n、m(1 ≤ n, m ≤ 1000)和 k(1 ≤ k ≤ 1018)——分别表示堆放干草堆的矩形表格的行数、列数,以及重新整理后所需的干草总体积(单位:立方米)。
接下来是 n 行,每行包含 m 个正整数 ai,j(1 ≤ ai,j ≤ 109),其中 ai,j 表示表格中第 i 行、第 j 列位置上的干草堆的体积(单位:立方米)。
输出格式
In the first line print "YES" (without quotes), if Polycarpus can perform the reorganisation and "NO" (without quotes) otherwise. If the answer is "YES" (without quotes), then in next n lines print m numbers — the heights of the remaining hay stacks. All the remaining non-zero values should be equal, represent a connected area and at least one of these values shouldn't be altered.
If there are multiple answers, print any of them.
第一行输出 "YES"(不带引号),如果波利卡普斯能够完成重新组织;否则输出 "NO"(不带引号)。若答案为 "YES"(不带引号),则接下来的 n 行中,每行输出 m 个数字——即剩余干草堆的高度。所有剩余的非零值必须相等,构成一个连通区域,且其中至少有一个值未被改动。
若存在多个合法答案,输出任意一个即可。
输入输出样例
输入#1
2 3 35 10 4 9 9 9 7
输出#1
YES 7 0 7 7 7 7
输入#2
4 4 50 5 9 1 1 5 1 1 5 5 1 5 5 5 5 7 1
输出#2
YES 5 5 0 0 5 0 0 5 5 0 5 5 5 5 5 0
输入#3
2 4 12 1 1 3 1 1 6 2 4
输出#3
NO
说明/提示
In the first sample non-zero values make up a connected area, their values do not exceed the initial heights of hay stacks. All the non-zero values equal 7, and their number is 5, so the total volume of the remaining hay equals the required value k = 7·5 = 35. At that the stack that is on the second line and third row remained unaltered.
在第一个样例中,非零值构成一个连通区域,且这些值均未超过干草堆的初始高度。所有非零值均为 7,其数量为 5,因此剩余干草的总体积等于所要求的值 k=7⋅5=35。此时,位于第二行第三列的干草堆保持不变。
输入解题思路,AI测评打分。不知道怎么写?