CF1774D.Same Count One
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
ChthollyNotaSeniorious received a special gift from AquaMoon: n binary arrays of length m. AquaMoon tells him that in one operation, he can choose any two arrays and any position pos from 1 to m, and swap the elements at positions pos 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 1s 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.
ChthollyNotaSeniorious 收到了 AquaMoon 送来的特殊礼物:n 个长度为 m 的二进制数组。AquaMoon 告诉他,在一次操作中,他可以任选两个数组以及一个位置 pos(1≤pos≤m),然后交换这两个数组在位置 pos 处的元素。
他对这个游戏非常着迷,并希望找出使所有数组中 1 的个数相等所需的最少操作次数。他邀请你一同参与这个有趣的游戏,请你尝试求解!
如果可行,请按输出部分所描述的格式输出具体的交换步骤;否则,请输出 −1。
输入格式
The first line of the input contains a single integer t (1≤t≤2⋅104) — the number of test cases. The description of test cases follows.
The first line of each test case contains two integers n and m (2≤n≤105, 2≤m≤105).
The i-th of the following n lines contains m integers ai,1,ai,2,…,ai,m (0≤ai,j≤1) — the elements of the i-th array.
It is guaranteed that the sum of n⋅m over all test cases does not exceed 106.
输入的第一行包含一个整数 t(1≤t≤2⋅104),表示测试用例的数量。接下来是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 m(2≤n≤105,2≤m≤105)。
接下来的 n 行中,第 i 行包含 m 个整数 ai,1,ai,2,…,ai,m(0≤ai,j≤1),表示第 i 个数组的元素。
保证所有测试用例中 n⋅m 的总和不超过 106。
输出格式
For each test case, if the objective is not achievable, output −1.
Otherwise, in the first line output k (0≤k≤mn) — the minimum number of operations required.
The i-th of the following k lines should contain 3 integers, xi,yi,zi (1≤xi,yi≤n,1≤zi≤m), which describe an operation that swap axi,zi,ayi,zi: swap the zi-th number of the xi-th and yi-th arrays.
对于每个测试用例,如果目标无法达成,则输出 −1。
否则,第一行输出 k (0≤k≤mn) —— 所需的最少操作次数。
接下来的 k 行中,第 i 行应包含 3 个整数 xi,yi,zi (1≤xi,yi≤n,1≤zi≤m),描述一次操作:交换 axi,zi 与 ayi,zi,即交换第 xi 个数组和第 yi 个数组的第 zi 个数。
输入输出样例
输入#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], each of them contains exactly two 1s.
在第一个测试用例中,只需执行一次操作:交换第二行和第一行的第一个元素。数组将变为 [0,1,1,0],[1,0,1,0],[1,0,0,1],每个数组都恰好包含两个 1。
输入解题思路,AI测评打分。不知道怎么写?