CF1720E.Misha and Paintings

省选/NOI-

通过率:0%

时间限制:3.50s

内存限制:256MB

AC君温馨提醒

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

题目描述

Misha has a square n×nn \times n matrix, where the number in row ii and column jj is equal to ai,ja_{i, j}. Misha wants to modify the matrix to contain exactly kk distinct integers. To achieve this goal, Misha can perform the following operation zero or more times:

  1. choose any square submatrix of the matrix (you choose (x1,y1)(x_1,y_1), (x2,y2)(x_2,y_2), such that x1≤x2x_1 \leq x_2, y1≤y2y_1 \leq y_2, x2−x1=y2−y1x_2 - x_1 = y_2 - y_1, then submatrix is a set of cells with coordinates (x,y)(x, y), such that x1≤x≤x2x_1 \leq x \leq x_2, y1≤y≤y2y_1 \leq y \leq y_2),
  2. choose an integer kk, where 1≤k≤n21 \leq k \leq n^2,
  3. replace all integers in the submatrix with kk.

Please find the minimum number of operations that Misha needs to achieve his goal.

米沙有一个 n×nn \times n 的方阵,其中第 ii 行第 jj 列的数为 ai,ja_{i, j}。米沙希望修改该矩阵,使其恰好包含 kk 个互不相同的整数。为达成这一目标,米沙可以执行以下操作零次或多次:

  1. 任选矩阵的一个正方形子矩阵(即选定 (x1,y1)(x_1,y_1) 和 (x2,y2)(x_2,y_2),满足 x1≤x2x_1 \leq x_2、y1≤y2y_1 \leq y_2 且 x2−x1=y2−y1x_2 - x_1 = y_2 - y_1;该子矩阵由所有满足 x1≤x≤x2x_1 \leq x \leq x_2、y1≤y≤y2y_1 \leq y \leq y_2 的单元格 (x,y)(x, y) 构成);
  2. 选择一个整数 kk,满足 1≤k≤n21 \leq k \leq n^2;
  3. 将该子矩阵中所有整数替换为 kk。

请找出米沙达成目标所需的最少操作次数。

输入格式

The first input line contains two integers nn and kk (1≤n≤500,1≤k≤n21 \leq n \leq 500, 1 \leq k \leq n^2) — the size of the matrix and the desired amount of distinct elements in the matrix.

Then nn lines follows. The ii-th of them contains nn integers ai,1,ai,2,…,ai,na_{i, 1}, a_{i, 2}, \ldots, a_{i, n} (1≤ai,j≤n21 \leq a_{i,j} \leq n^2) — the elements of the ii-th row of the matrix.

第一行输入包含两个整数 nn 和 kk(1≤n≤5001 \leq n \leq 500,1≤k≤n21 \leq k \leq n^2)—— 分别表示矩阵的大小以及矩阵中期望的不同元素个数。

接下来是 nn 行。其中第 ii 行包含 nn 个整数 ai,1,ai,2,…,ai,na_{i, 1}, a_{i, 2}, \ldots, a_{i, n}(1≤ai,j≤n21 \leq a_{i,j} \leq n^2)—— 表示矩阵第 ii 行的元素。

输出格式

Output one integer — the minimum number of operations required.

输出一个整数——所需的最少操作次数。

输入输出样例

  • 输入#1

    3 4
    1 1 1
    1 1 2
    3 4 5

    输出#1

    1
  • 输入#2

    3 2
    2 1 3
    2 1 1
    3 1 2

    输出#2

    2
  • 输入#3

    3 3
    1 1 1
    1 1 2
    2 2 2

    输出#3

    1
  • 输入#4

    3 2
    1 1 1
    1 2 1
    2 2 2

    输出#4

    0

说明/提示

In the first test case the answer is 11, because one can change the value in the bottom right corner of the matrix to 11. The resulting matrix can be found below:

1

1

1

1

1

2

3

4

1

In the second test case the answer is 22. First, one can change the entire matrix to contain only 11s, and the change the value of any single cell to 22. One of the possible resulting matrices is displayed below:

1

1

1

1

1

1

1

1

2

在第一个测试用例中,答案为 11,因为可以将矩阵右下角的值修改为 11。得到的矩阵如下所示:

1

1

1

1

1

2

3

4

1

在第二个测试用例中,答案为 22。首先,可以将整个矩阵的所有元素都改为 11,然后将任意一个单元格的值改为 22。其中一个可能得到的矩阵如下所示:

1

1

1

1

1

1

1

1

2

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

首页