A16432 | Matrix Problem
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a matrix $a$ , consisting of $n$ rows by $m$ columns. Each element of the matrix is equal to $0$ or $1$ .
You can perform the following operation any number of times (possibly zero): choose an element of the matrix and replace it with either $0$ or $1$ .
You are also given two arrays $A$ and $B$ (of length $n$ and $m$ respectively). After you perform the operations, the matrix should satisfy the following conditions:
1. the number of ones in the $i$ -th row of the matrix should be exactly $A_i$ for every $i \in [1, n]$ .
2. the number of ones in the $j$ -th column of the matrix should be exactly $B_j$ for every $j \in [1, m]$ .
Calculate the minimum number of operations you have to perform.
You can perform the following operation any number of times (possibly zero): choose an element of the matrix and replace it with either $0$ or $1$ .
You are also given two arrays $A$ and $B$ (of length $n$ and $m$ respectively). After you perform the operations, the matrix should satisfy the following conditions:
1. the number of ones in the $i$ -th row of the matrix should be exactly $A_i$ for every $i \in [1, n]$ .
2. the number of ones in the $j$ -th column of the matrix should be exactly $B_j$ for every $j \in [1, m]$ .
Calculate the minimum number of operations you have to perform.
输入格式
The first line contains two integers $n$ and $m$ ( $2 \le n, m \le 50$ ).
Then $n$ lines follow. The $i$ -th of them contains $m$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,m}$ ( $0 \le a_{i,j} \le 1$ ).
The next line contains $n$ integers $A_1, A_2, \dots, A_n$ ( $0\le A_i\le m$ ).
The next line contains $m$ integers $B_1, B_2, \dots, B_m$ ( $0\le B_i\le n$ ).
Then $n$ lines follow. The $i$ -th of them contains $m$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,m}$ ( $0 \le a_{i,j} \le 1$ ).
The next line contains $n$ integers $A_1, A_2, \dots, A_n$ ( $0\le A_i\le m$ ).
The next line contains $m$ integers $B_1, B_2, \dots, B_m$ ( $0\le B_i\le n$ ).
输出格式
Print one integer — the minimum number of operations you have to perform, or -1 if it is impossible.
输入输出样例
输入 #1
3 3 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1
输出 #1
3
输入 #2
3 3 1 1 1 1 1 1 1 1 1 3 2 1 1 2 3
输出 #2
3
输入 #3
2 2 0 0 0 0 1 2 0 1
输出 #3
-1
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted