Gerrymandering 2
Time limit1sMemory limit512 MB
For an N x N grid, try every valid 5-way district split defined by a reference point and two boundary lengths, and report the smallest difference between the largest and smallest district populations.
- Level
Medium6 of 10
- Topics
- Brute force, Simulation, Implementation, Prefix sum
- Solved
- No attempts yet
Problem
Gujehyeon, the mayor of Jaehyeon City, has spent the past few years gerrymandering districts to favor his own party. With no power left to check him, he exercised his authority very unfairly, and even renamed the city to Jaehyeon City. For this election, he wants to draw the districts as fairly as possible.
Jaehyeon City can be represented as an N×N grid. Each cell is a district, and the district in row r, column c is written (r, c). The districts must be divided into five electoral districts, and each district must belong to one of the five. An electoral district must contain at least one district, and all districts that belong to one electoral district must be connected. Two districts A and B are connected when you can travel from A to B through adjacent districts. The intermediate adjacent districts number zero or more, and all of them must belong to the same electoral district.
The method of dividing the electoral districts is as follows.
-
Choose a reference point (x, y) and the boundary lengths d1 and d2. (d1, d2 ≥ 1, 1 ≤ x < x+d1+d2 ≤ N, 1 ≤ y-d1 < y < y+d2 ≤ N)
-
The following cells are boundary lines.
- Boundary line 1: (x, y), (x+1, y-1), ..., (x+d1, y-d1)
- Boundary line 2: (x, y), (x+1, y+1), ..., (x+d2, y+d2)
- Boundary line 3: (x+d1, y-d1), (x+d1+1, y-d1+1), ... (x+d1+d2, y-d1+d2)
- Boundary line 4: (x+d2, y+d2), (x+d2+1, y+d2-1), ..., (x+d2+d1, y+d2-d1)
-
The cells on the boundary lines and inside them form electoral district 5.
-
For a district (r, c) not included in electoral district 5, its electoral district number follows these rules.
- Electoral district 1: 1 ≤ r < x+d1, 1 ≤ c ≤ y, upper left of boundary line 1
- Electoral district 2: 1 ≤ r ≤ x+d2, y < c ≤ N, upper right of boundary line 2
- Electoral district 3: x+d1 ≤ r ≤ N, 1 ≤ c < y-d1+d2, lower left of boundary line 3
- Electoral district 4: x+d2 < r ≤ N, y-d1+d2 ≤ c ≤ N, lower right of boundary line 4
Below are examples of dividing a 7×7 Jaehyeon City into five electoral districts.
The population of district (r, c) is A[r][c], and the population of an electoral district is the sum of the populations of the districts it contains. Among all ways of dividing the electoral districts, find the minimum of the difference in population between the most populous electoral district and the least populous electoral district.
Input
The first line gives the size N of Jaehyeon City.
From the second line, N lines each give N integers. The integer in row r, column c is A[r][c].
Output
Print on the first line the minimum of the difference in population between the most populous electoral district and the least populous electoral district.
Constraints
- 5 ≤ N ≤ 20
- 1 ≤ A[r][c] ≤ 100


