CF1913E.Matrix Problem

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a matrix aa, consisting of nn rows by mm columns. Each element of the matrix is equal to 00 or 11.

You can perform the following operation any number of times (possibly zero): choose an element of the matrix and replace it with either 00 or 11.

You are also given two arrays AA and BB (of length nn and mm respectively). After you perform the operations, the matrix should satisfy the following conditions:

  1. the number of ones in the ii-th row of the matrix should be exactly AiA_i for every i∈[1,n]i \in [1, n].
  2. the number of ones in the jj-th column of the matrix should be exactly BjB_j for every j∈[1,m]j \in [1, m].

Calculate the minimum number of operations you have to perform.

给你一个 nn 行 mm 列的矩阵 aa,其中每个元素均为 00 或 11。

你可以执行以下操作任意次(包括零次):选择矩阵中的某个元素,并将其替换为 00 或 11。

同时给你两个数组 AA 和 BB(长度分别为 nn 和 mm)。在执行若干次操作后,矩阵需满足以下条件:

  1. 对每个 i∈[1,n]i \in [1, n],矩阵第 ii 行中 11 的个数恰好为 AiA_i;
  2. 对每个 j∈[1,m]j \in [1, m],矩阵第 jj 列中 11 的个数恰好为 BjB_j。

请计算所需执行的最少操作次数。

输入格式

The first line contains two integers nn and mm (2≤n,m≤502 \le n, m \le 50).

Then nn lines follow. The ii-th of them contains mm integers ai,1,ai,2,…,ai,ma_{i,1}, a_{i,2}, \dots, a_{i,m} (0≤ai,j≤10 \le a_{i,j} \le 1).

The next line contains nn integers A1,A2,…,AnA_1, A_2, \dots, A_n (0≤Ai≤m0\le A_i\le m).

The next line contains mm integers B1,B2,…,BmB_1, B_2, \dots, B_m (0≤Bi≤n0\le B_i\le n).

第一行包含两个整数 nn 和 mm(2≤n,m≤502 \le n, m \le 50)。

接下来是 nn 行。其中第 ii 行包含 mm 个整数 ai,1,ai,2,…,ai,ma_{i,1}, a_{i,2}, \dots, a_{i,m}(0≤ai,j≤10 \le a_{i,j} \le 1)。

下一行包含 nn 个整数 A1,A2,…,AnA_1, A_2, \dots, A_n(0≤Ai≤m0\le A_i\le m)。

再下一行包含 mm 个整数 B1,B2,…,BmB_1, B_2, \dots, B_m(0≤Bi≤n0\le B_i\le n)。

输出格式

Print one integer — the minimum number of operations you have to perform, or -1 if it is impossible.

输出一个整数——你需要执行的最少操作次数;如果不可能,则输出 −1-1。

输入输出样例

  • 输入#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

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

首页