問題一覧 > 通常問題

No.1668 Grayscale

レベル : / AI推定難易度 : 1525 / 実行時間制限 : 1ケース 4.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 65
作問者 : riano / テスター : noimi
お気に入りにしたユーザー ProblemId : 6312 / 自分の提出
問題文最終更新日: 2021-12-30 00:06:55
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 312 (順位表) の他の問題:

Problem Statement

You have one black-and-white image.
Although it is called black-and-white, it is expressed as a gradation of grays of different darkness, and it contains $N$ kinds of colors. These colors are represented by a value called "gray darkness", and $2$ grays are considered the same color only when these values are equal.
The image consists of cells in $H$ rows and $W$ columns, each painted in a color, and the "gray darkness" of cell $(i,j)$, in the $i$-th row from the top and the $j$-th column from the left, is $C_{ij}$. You want to convert this image into an image with as few colors as possible.
Here, a conversion of the original image means generating an image with $H$ rows and $W$ columns, where the "gray darkness" of cell $(i,j)$, in the $i$-th row from the top and the $j$-th column from the left, is $D_{ij}$, that satisfies the following conditions.

■Conditions
1. For any $2$ colors, the order of their "gray darkness" is not reversed between the image before the conversion and the image after the conversion.
 Strictly, for any $2$ cells $(i,j)$, $(k,l)$, if $C_{ij}≤C_{kl}$ then $D_{ij}≤D_{kl}$.
2. The "color boundaries" do not change through the conversion.
 That is, for any $2$ cells adjacent by sharing an edge, if their "gray darkness" differs before the conversion, then it also differs after the conversion.

Find the minimum number of kinds of colors appearing in the converted image, over all conversions satisfying the conditions.

Input

$H$ $W$ $N$
$C_{11}$ $C_{12}$ ... $C_{1W}$
$C_{21}$ $C_{22}$ ... $C_{2W}$
... 
$C_{H1}$ $C_{H2}$ ... $C_{HW}$

$H$ is the number of rows of cells forming the image, $W$ is the number of columns, and $N$ is the number of kinds of colors appearing in the image before the conversion.
$C_{ij}$ represents the "gray darkness" of cell $(i,j)$, in the $i$-th row from the top and the $j$-th column from the left.

■Constraints
$1≤H,W≤1000$
$1≤N≤HW$
$1≤C_{ij}≤N$
For any $k$ satisfying $1≤k≤N$, there exists a cell $(i,j)$ such that $C_{ij}=k$.
All inputs are integers.

Output

Output the minimum number of kinds of colors of an output image satisfying the conditions of the problem.
Print a newline at the end.

Sample

Sample 1
Input
2 2 4
1 2
3 4
Output
3

Let us call the color whose gray darkness is $k$ color $k$.
Even if color $2$ and color $3$ are both changed to $2$, the conditions can be satisfied.
Indeed, you can see that the edges that are color boundaries remain color boundaries, and the order of gray darkness is not reversed either.
Trying to reduce the colors any further violates the conditions; for example, converting $1$ to $2$ makes a color boundary no longer a boundary.
Therefore, $3$ kinds of colors ($1,2,4$ in the example of this explanation) is the minimum satisfying the conditions, so answer $3$.

Sample 2
Input
3 3 7
1 4 5
7 7 7
2 3 6
Output
4

Colors $1$ and $2$, $3$ and $4$, and $5$ and $6$ may each be made the same color.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。