102033: [AtCoder]ABC203 D - Pond

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $400$ points

Problem Statement

The land of a park AtCoder is an $N\times N$ grid with east-west rows and north-south columns. The height of the square at the $i$-th row from the north and $j$-th column from the west is given as $A_{i,j}$.

Takahashi, the manager, has decided to build a square pond occupying $K \times K$ squares in this park.

To do this, he wants to choose a square section of $K \times K$ squares completely within the park whose median of the heights of the squares is the lowest. Find the median of the heights of the squares in such a section.

Here, the median of the heights of the squares in a $K \times K$ section is the height of the $(\left\lfloor\frac{K^2}{2}\right\rfloor +1)$-th highest square among the $K^2$ squares in the section, where $\lfloor x\rfloor$ is the greatest integer not exceeding $x$.

Constraints

  • $1 \leq K \leq N \leq 800$
  • $0 \leq A_{i,j} \leq 10^9$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$ $K$
$A_{1,1}$ $A_{1,2}$ $\ldots$ $A_{1,N}$
$A_{2,1}$ $A_{2,2}$ $\ldots$ $A_{2,N}$
$:$
$A_{N,1}$ $A_{N,2}$ $\ldots$ $A_{N,N}$

Output

Print the answer.


Sample Input 1

3 2
1 7 0
5 8 11
10 4 2

Sample Output 1

4

Let $(i,j)$ denote the square at the $i$-th row from the north and $j$-th column from the west. We have four candidates for the $2 \times 2$ section occupied by the pond: $\{(1,1),(1,2),(2,1),(2,2)\}, \{(1,2),(1,3),(2,2),(2,3)\}, \{(2,1),(2,2),(3,1),(3,2)\}, \{(2,2),(2,3),(3,2),(3,3)\}$.
When $K=2$, since $\left\lfloor\frac{2^2}{2}\right\rfloor+1=3$, the median of the heights of the squares in a section is the height of the $3$-rd highest square, which is $5$, $7$, $5$, $4$ for the candidates above, respectively. We should print the lowest of these: $4$.


Sample Input 2

3 3
1 2 3
4 5 6
7 8 9

Sample Output 2

5

Input

题意翻译

### 题目大意 给定一个 $n\times n$ 的矩阵 $A$,再给定一个数 $k$,求矩阵中所有大小为 $k\times k$ 的子矩阵的中位数的最小值。 一个 $k\times k$ 的矩阵的中位数被定义为将矩阵中的所有数从大到小排序后的第 $\lfloor\frac{k^2}{2}\rfloor+1$ 个数。 ### 输入格式 第一行两个正整数 $n,k$。 接下来 $n$ 行,每行 $n$ 个数,描述了一个矩阵。 ### 输出格式 输出一行一个数,表示中位数的最小值。 ### 说明/提示 $1\le k\le n\le 800,0\le A_{i,j}\le 10^9$。 Translated by \_Ponder_

加入题单

上一题 下一题 算法标签: