PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000482. Chất lượng cuộc sống (Quality of Living)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 RR rows and CC columns. Rows are numbered from 00 to R−1R-1 from north to south, and columns from 00 to C−1C-1 from west to east.

Block (a,b)(a,b) has quality rank Qa,bQ_{a,b}, with a lower rank indicating better living conditions. All ranks are distinct integers from 11 to R⋅CR\cdot C.

The planning department must select a rectangle of exactly HH consecutive rows and WW 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 HW+12\frac{HW+1}{2} (counting from 1). There are equally many ranks better and worse than this median.

Find the minimum median over all rectangles of dimensions exactly H×WH\times W.

Input

The first line contains four integers R,C,H,WR,C,H,W: the grid dimensions followed by the dimensions of the required rectangle.

The next RR lines each contain CC integers. Line a+1a+1 of this block contains Qa,0,Qa,1,…,Qa,C−1Q_{a,0},Q_{a,1},\ldots,Q_{a,C-1}, the ranks of the blocks in row aa in order.

Output

Print one integer: the smallest attainable median.

Subtasks

  • Subtask 1 (20 points): 1≤R,C≤301\le R,C\le 30; 1≤H≤R1\le H\le R; 1≤W≤C1\le W\le C; H,WH,W are odd integers; Qa,bQ_{a,b} is an integer with 1≤Qa,b≤RC1\le Q_{a,b}\le RC for all 0≤a<R0\le a<R, 0≤b<C0\le b<C; all RCRC ranks are distinct; R,CR,C are integers.

  • Subtask 2 (20 points): 1≤R,C≤1001\le R,C\le 100; 1≤H≤R1\le H\le R; 1≤W≤C1\le W\le C; H,WH,W are odd integers; Qa,bQ_{a,b} is an integer with 1≤Qa,b≤RC1\le Q_{a,b}\le RC for all 0≤a<R0\le a<R, 0≤b<C0\le b<C; all RCRC ranks are distinct; R,CR,C are integers.

  • Subtask 3 (20 points): 1≤R,C≤3001\le R,C\le 300; 1≤H≤R1\le H\le R; 1≤W≤C1\le W\le C; H,WH,W are odd integers; Qa,bQ_{a,b} is an integer with 1≤Qa,b≤RC1\le Q_{a,b}\le RC for all 0≤a<R0\le a<R, 0≤b<C0\le b<C; all RCRC ranks are distinct; R,CR,C are integers.

  • Subtask 4 (20 points): 1≤R,C≤10001\le R,C\le 1000; 1≤H≤R1\le H\le R; 1≤W≤C1\le W\le C; H,WH,W are odd integers; Qa,bQ_{a,b} is an integer with 1≤Qa,b≤RC1\le Q_{a,b}\le RC for all 0≤a<R0\le a<R, 0≤b<C0\le b<C; all RCRC ranks are distinct; R,CR,C are integers.

  • Subtask 5 (20 points): 1≤R,C≤30001\le R,C\le 3000; 1≤H≤R1\le H\le R; 1≤W≤C1\le W\le C; H,WH,W are odd integers; Qa,bQ_{a,b} is an integer with 1≤Qa,b≤RC1\le Q_{a,b}\le RC for all 0≤a<R0\le a<R, 0≤b<C0\le b<C; all RCRC ranks are distinct; R,CR,C 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 3×33\times3 windows have medians 12,12,10,19,18,9,20,19,1312,12,10,19,18,9,20,19,13 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 1,2,3,7,9,10,14,19,201,2,3,7,9,10,14,19,20. 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 6,5,9,86,5,9,8. The window at row 0, columns 1–5, contains 1,2,11,7,51,2,11,7,5, which sorts to 1,2,5,7,111,2,5,7,11. The middle rank 5 is the smallest of the four medians.