A15611 | Same Count One
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $1$ s 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$ .
He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of $1$ s 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$ .
输入格式
The first line of the input contains a single integer $t$ ( $1 \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 $n$ and $m$ ( $2 \leq n \leq 10^5$ , $2 \leq m \leq 10^5$ ).
The $i$ -th of the following $n$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$ $(0 \le a_{i, j} \le 1)$ — the elements of the $i$ -th array.
It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$ .
The first line of each test case contains two integers $n$ and $m$ ( $2 \leq n \leq 10^5$ , $2 \leq m \leq 10^5$ ).
The $i$ -th of the following $n$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$ $(0 \le a_{i, j} \le 1)$ — the elements of the $i$ -th array.
It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$ .
输出格式
For each test case, if the objective is not achievable, output $-1$ .
Otherwise, in the first line output $k$ $(0 \le k \le mn)$ — the minimum number of operations required.
The $i$ -th of the following $k$ lines should contain $3$ integers, $x_i, y_i, z_i$ $(1 \le x_i, y_i \le n, 1 \le z_i \le m)$ , which describe an operation that swap $a_{x_i, z_i}, a_{y_i, z_i}$ : swap the $z_i$ -th number of the $x_i$ -th and $y_i$ -th arrays.
Otherwise, in the first line output $k$ $(0 \le k \le mn)$ — the minimum number of operations required.
The $i$ -th of the following $k$ lines should contain $3$ integers, $x_i, y_i, z_i$ $(1 \le x_i, y_i \le n, 1 \le z_i \le m)$ , which describe an operation that swap $a_{x_i, z_i}, a_{y_i, z_i}$ : swap the $z_i$ -th number of the $x_i$ -th and $y_i$ -th arrays.
输入输出样例
输入 #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 $1$ s.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted