#P2216. [HAOI2007] 理想的正方形

    ID: 1190 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2007河南各省省选单调队列RMQ队列

[HAOI2007] 理想的正方形

Description

Given an a×ba \times b matrix of integers, find an n×nn \times n square region such that the difference between the maximum and minimum numbers in that region is minimized.

Input Format

The first line contains 3 integers, representing the values of aa, bb, and nn.

From the second line to the (a+1)(a+1)-th line, each line contains bb nonnegative integers, representing the numbers at the corresponding positions in the matrix. Adjacent numbers on the same line are separated by a single space.

Output Format

Output a single integer: the minimal possible difference between the maximum and the minimum among all n×nn \times n square regions in the a×ba \times b matrix.

5 4 2
1 2 5 6
0 17 16 0
16 17 2 1
2 10 2 1
1 2 2 2

1

Hint

All numbers in the matrix do not exceed 1,000,000,000.

Constraints:

For 20% of the testdata, 2a,b1002 \le a, b \le 100, nan \le a, nbn \le b, n10n \le 10.

For 100% of the testdata, 2a,b10002 \le a, b \le 1000, nan \le a, nbn \le b, n100n \le 100.

Translated by ChatGPT 5