#BS0000482. Chất lượng cuộc sống (Quality of Living)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chất lượng cuộc sống (Quality of Living)
Quality of Living
Source: IOI
Version: Phuoc Hung OJ Extended
Problem Statement
The city blocks in an Alberta city form a rectangular grid of rows and columns. Rows are numbered from to from north to south, and columns from to from west to east.
Block has quality rank , with a lower rank indicating better living conditions. All ranks are distinct integers from to .
The planning department must select a rectangle of exactly consecutive rows and consecutive columns, entirely within the grid. Both dimensions are odd, so the selected rectangle contains an odd number of blocks.
The median of an odd-sized collection of ranks is its middle element in increasing order. In a selected rectangle, it is the rank at position (counting from 1). There are equally many ranks better and worse than this median.
Find the minimum median over all rectangles of dimensions exactly .
Input
The first line contains four integers : the grid dimensions followed by the dimensions of the required rectangle.
The next lines each contain integers. Line of this block contains , the ranks of the blocks in row in order.
Output
Print one integer: the smallest attainable median.
Subtasks
-
Subtask 1 (20 points): ; ; ; are odd integers; is an integer with for all , ; all ranks are distinct; are integers.
-
Subtask 2 (20 points): ; ; ; are odd integers; is an integer with for all , ; all ranks are distinct; are integers.
-
Subtask 3 (20 points): ; ; ; are odd integers; is an integer with for all , ; all ranks are distinct; are integers.
-
Subtask 4 (20 points): ; ; ; are odd integers; is an integer with for all , ; all ranks are distinct; are integers.
-
Subtask 5 (20 points): ; ; ; are odd integers; is an integer with for all , ; all ranks are distinct; are integers.
Examples
Input 1
5 5 3 3
5 11 12 16 25
17 18 2 7 10
4 23 20 3 1
24 21 19 14 9
6 22 8 13 15
Output 1
9
Explanation 1
The nine windows have medians in row-major order of their top-left corners.
The window starting at row 1, column 2 (zero-based) covers rows 1–3 and columns 2–4. Its sorted ranks are . The fifth rank is 9, the smallest of all nine medians, so print 9.
Input 2
2 6 1 5
6 1 2 11 7 5
9 3 4 10 12 8
Output 2
5
Explanation 2
Each row contains two horizontal windows of length five. Their medians are . The window at row 0, columns 1–5, contains , which sorts to . The middle rank 5 is the smallest of the four medians.