QOJ.ac

QOJ

حد الوقت: 2.0 s حد الذاكرة: 1024 MB مجموع النقاط: 100 قابلة للهجوم ✓

#15484. 城市平衡器

الإحصائيات

每年,泡泡王国都会在大广场展示其壮观的泡泡矩阵(Bubble Matrix),其中每个单元格代表王国中某个特定城市的泡泡度(bubbliness)。泡泡度用泡泡值(Bubble Points,简称 BP)来量化。

作为泡泡指标的皇家顾问,你发现王国的泡泡矩阵存在失衡。有些城市的泡泡度溢出,而另一些城市则远远落后。国王赋予你的任务是,确保每行中所有城市的 BP 之和相同,且每列中所有城市的 BP 之和也相同。

你有一个大小为 $N \times M$ 的矩阵 $A_{i,j}$,每个数字代表对应城市的泡泡值。为了恢复平衡,你被允许执行最多 $2 \cdot (N + M)$ 次“泡泡操作”(Bubble Operation)。

泡泡操作的定义如下:

  • 选择矩阵中的一个单元格 $A_{i,j}$ 并选择一个值 $V$。
  • 从 $A_{i,j}$ 的所有边相邻(edge-connected)的邻居中减去 $V$。
  • 将减去的这些值加到 $A_{i,j}$ 上。

需要特别注意的是,在每次泡泡操作后,整个矩阵的泡泡值总和将始终保持不变。你的目标是执行一个长度最多为 $2 \cdot (N + M)$ 的泡泡操作序列,以确保任意给定行中所有城市的 BP 之和等于任何其他行中城市的 BP 之和,且任意给定列中所有城市的 BP 之和等于任何其他列中城市的 BP 之和。如果无法做到这一点,请指出。

输入格式

第一行包含两个整数 $N$ 和 $M$:泡泡矩阵的行数和列数($1 \le N, M \le 10^3$)。

接下来的 $N$ 行,每行包含 $M$ 个整数:每个城市的初始泡泡值 $A_{i,j}$($-10^3 \le A_{i,j} \le 10^3$)。

输出格式

如果无法平衡矩阵,输出单行,包含 $-1$。

否则,首先输出一行,包含一个整数 $K$,表示你执行的泡泡操作次数($0 \le K \le 2 \cdot (N + M)$,你不需要最小化它)。

接下来输出 $K$ 行,每行格式为 Ri Ci Vi,其中整数 $R_i$ 和 $C_i$ 表示你在第 $i$ 次操作中选择的 $A_{R_i, C_i}$ 的 1-based 坐标,整数 $V_i$ 是该操作中使用的值 $V$($1 \le R_i \le N$,$1 \le C_i \le M$,$-5 \cdot 10^{14} \le V_i \le 5 \cdot 10^{14}$)。

样例

输入样例 1

2 3
0 3 3
4 -1 3

输出样例 1

2
1 3 -1
2 3 -1

说明

在样例中,第一次操作后,矩阵如下所示:

$$ \begin{pmatrix} 0 & 4 & 1 \\ 4 & -1 & 4 \end{pmatrix} $$

第二次操作后,矩阵如下所示:

$$ \begin{pmatrix} 0 & 4 & 2 \\ 4 & 0 & 2 \end{pmatrix} $$

此时,每列的列和均为 $4$,而每行的行和均为 $6$。

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.