CF1662J.Training Camp

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are organizing a training camp to teach algorithms to young kids. There are n2n^2 kids, organized in an nn by nn grid. Each kid is between 11 and nn years old (inclusive) and any two kids who are in the same row or in the same column have different ages.

You want to select exactly nn kids for a programming competition, with exactly one kid from each row and one kid from each column. Moreover, kids who are not selected must be either older than both kids selected in their row and column, or younger than both kids selected in their row and column (otherwise they will complain). Notice that it is always possible to select nn kids satisfying these requirements (for example by selecting nn kids who have the same age).

During the training camp, you observed that some kids are good at programming, and the others are not. What is the maximum number of kids good at programming that you can select while satisfying all the requirements?

你正在组织一场训练营,向孩子们教授算法。共有 n2n^2 名孩子,排列成一个 n×nn \times n 的方阵。每名孩子的年龄在 11 到 nn 岁之间(含端点),且同一行或同一列中的任意两名孩子的年龄均不相同。

你需要恰好选出 nn 名孩子参加编程竞赛,要求:每行恰好选一名孩子,每列也恰好选一名孩子。此外,未被选中的孩子必须满足以下条件之一:其年龄严格大于其所在行与所在列中被选中的两名孩子的年龄,或者严格小于这两名被选中孩子的年龄(否则他们会抱怨)。注意,总存在满足这些要求的 nn 人选择方案(例如,选择 nn 名年龄完全相同的孩子即可)。

在训练营期间,你观察到部分孩子擅长编程,其余则不擅长。在满足上述所有要求的前提下,你最多能选出多少名擅长编程的孩子?

输入格式

The first line contains nn (1≤n≤1281 \leq n \leq 128) — the size of the grid.

The following nn lines describe the ages of the kids. Specifically, the ii-th line contains nn integers ai,1, ai,2, …, ai,na_{i,1}, \, a_{i,2}, \, \dots, \, a_{i, n} (1≤ai,j≤n1 \le a_{i,j} \le n) — where ai,ja_{i,j} is the age of the kid in the ii-th row and jj-th column. It is guaranteed that two kids on the same row or column have different ages, i.e., ai,j≠ai,j′a_{i,j} \ne a_{i,j'} for any 1≤i≤n1\le i\le n, 1≤j<j′≤n1\le j \lt j'\le n, and ai,j≠ai′,ja_{i,j} \ne a_{i',j} for any 1≤i<i′≤n1\le i \lt i'\le n, 1≤j≤n1\le j\le n.

The following nn lines describe the programming skills of the kids. Specifically, the ii-th line contains nn integers ci,1, ci,2, …, ci,nc_{i,1}, \, c_{i,2}, \, \dots, \, c_{i, n} (ci,j∈0, 1c_{i,j} \in {0, \, 1}) — where ci,j=1c_{i,j}=1 if the kid in the ii-th row and jj-th column is good at programming and ci,j=0c_{i,j}=0 otherwise.

第一行包含一个整数 nn(1≤n≤1281 \leq n \leq 128),表示网格的大小。

接下来的 nn 行描述孩子们的年龄。具体而言,第 ii 行包含 nn 个整数 ai,1, ai,2, …, ai,na_{i,1},\, a_{i,2},\, \dots,\, a_{i,n}(1≤ai,j≤n1 \le a_{i,j} \le n),其中 ai,ja_{i,j} 表示第 ii 行、第 jj 列的孩子的年龄。题目保证同一行或同一列上的任意两个孩子年龄均不相同,即对任意 1≤i≤n1\le i\le n、1≤j<j′≤n1\le j \lt j'\le n,有 ai,j≠ai,j′a_{i,j} \ne a_{i,j'};且对任意 1≤i<i′≤n1\le i \lt i'\le n、1≤j≤n1\le j\le n,有 ai,j≠ai′,ja_{i,j} \ne a_{i',j}。

接下来的 nn 行描述孩子们的编程能力。具体而言,第 ii 行包含 nn 个整数 ci,1, ci,2, …, ci,nc_{i,1},\, c_{i,2},\, \dots,\, c_{i,n}(ci,j∈{0, 1}c_{i,j} \in \{0,\, 1\}),其中若第 ii 行、第 jj 列的孩子擅长编程,则 ci,j=1c_{i,j}=1;否则 ci,j=0c_{i,j}=0。

输出格式

Print the maximum number of kids good at programming that you can select while satisfying all the requirements.

输出在满足所有要求的前提下,最多能选出多少名擅长编程的孩子。

输入输出样例

  • 输入#1

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

    输出#1

    1
  • 输入#2

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

    输出#2

    2

说明/提示

In the first sample, it is not possible to select the two kids good at programming (in row 11 and column 11, and in row 22 and column 33), because then you would have to select the kid in row 33 and column 22, and in that case two kids would complain (the one in row 11 and column 22, and the one in row 33 and column 11).

A valid selection which contains 11 kid good at programming is achieved by choosing the 33 kids who are 11 year old.

In the second sample, there are 1010 valid choices of the nn kids that satisfy the requirements, and each of them selects exactly 22 kids good at programming.

在第一个样例中,无法同时选择两名擅长编程的小孩(分别位于第 11 行第 11 列、以及第 22 行第 33 列),因为那样就必须同时选择第 33 行第 22 列的小孩;此时将有两名小孩提出抱怨(分别是第 11 行第 22 列和第 33 行第 11 列的小孩)。

一种包含 11 名擅长编程的小孩的有效选择方案是:选出全部 33 名年龄为 11 岁的小孩。

在第二个样例中,共有 1010 种满足要求的 nn 名小孩的选择方案,且每种方案均恰好选出 22 名擅长编程的小孩。

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

首页