CF1774D.Same Count One

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

ChthollyNotaSeniorious received a special gift from AquaMoon: nn binary arrays of length mm. AquaMoon tells him that in one operation, he can choose any two arrays and any position pospos from 11 to mm, and swap the elements at positions pospos in these arrays.

He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of 11s in all arrays the same. He has invited you to participate in this interesting game, so please try to find it!

If it is possible, please output specific exchange steps in the format described in the output section. Otherwise, please output −1-1.

ChthollyNotaSeniorious 收到了 AquaMoon 送来的特殊礼物:nn 个长度为 mm 的二进制数组。AquaMoon 告诉他,在一次操作中,他可以任选两个数组以及一个位置 pospos(1≤pos≤m1 \le pos \le m),然后交换这两个数组在位置 pospos 处的元素。

他对这个游戏非常着迷,并希望找出使所有数组中 11 的个数相等所需的最少操作次数。他邀请你一同参与这个有趣的游戏,请你尝试求解!

如果可行,请按输出部分所描述的格式输出具体的交换步骤;否则,请输出 −1-1。

输入格式

The first line of the input contains a single integer tt (1≤t≤2⋅1041 \leq t \leq 2\cdot 10^4) — the number of test cases. The description of test cases follows.

The first line of each test case contains two integers nn and mm (2≤n≤1052 \leq n \leq 10^5, 2≤m≤1052 \leq m \leq 10^5).

The ii-th of the following nn lines contains mm integers ai,1,ai,2,…,ai,ma_{i, 1}, a_{i, 2}, \ldots, a_{i, m} (0≤ai,j≤1)(0 \le a_{i, j} \le 1) — the elements of the ii-th array.

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

输入的第一行包含一个整数 tt(1≤t≤2⋅1041 \leq t \leq 2\cdot 10^4),表示测试用例的数量。接下来是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n≤1052 \leq n \leq 10^5,2≤m≤1052 \leq m \leq 10^5)。

接下来的 nn 行中,第 ii 行包含 mm 个整数 ai,1,ai,2,…,ai,ma_{i, 1}, a_{i, 2}, \ldots, a_{i, m}(0≤ai,j≤10 \le a_{i, j} \le 1),表示第 ii 个数组的元素。

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

输出格式

For each test case, if the objective is not achievable, output −1-1.

Otherwise, in the first line output kk (0≤k≤mn)(0 \le k \le mn) — the minimum number of operations required.

The ii-th of the following kk lines should contain 33 integers, xi,yi,zix_i, y_i, z_i (1≤xi,yi≤n,1≤zi≤m)(1 \le x_i, y_i \le n, 1 \le z_i \le m), which describe an operation that swap axi,zi,ayi,zia_{x_i, z_i}, a_{y_i, z_i}: swap the ziz_i-th number of the xix_i-th and yiy_i-th arrays.

对于每个测试用例,如果目标无法达成,则输出 −1-1。

否则,第一行输出 kk (0≤k≤mn)(0 \le k \le mn) —— 所需的最少操作次数。

接下来的 kk 行中,第 ii 行应包含 33 个整数 xi,yi,zix_i, y_i, z_i (1≤xi,yi≤n,1≤zi≤m)(1 \le x_i, y_i \le n, 1 \le z_i \le m),描述一次操作:交换 axi,zia_{x_i, z_i} 与 ayi,zia_{y_i, z_i},即交换第 xix_i 个数组和第 yiy_i 个数组的第 ziz_i 个数。

输入输出样例

  • 输入#1

    3
    3 4
    1 1 1 0
    0 0 1 0
    1 0 0 1
    4 3
    1 0 0
    0 1 1
    0 0 1
    0 0 0
    2 2
    0 0
    0 1

    输出#1

    1
    2 1 1
    1
    4 2 2
    -1

说明/提示

In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become [0,1,1,0],[1,0,1,0],[1,0,0,1][0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1], each of them contains exactly two 11s.

在第一个测试用例中,只需执行一次操作:交换第二行和第一行的第一个元素。数组将变为 [0,1,1,0],[1,0,1,0],[1,0,0,1][0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1],每个数组都恰好包含两个 11。

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

首页