Jurisdiction Restrictions
Time limit30sMemory limit1024 MB
Given a grid and police stations with square patrol ranges, assign each coverable block to one station so the gap between the largest and smallest counts is minimal.
- Level
Medium7 of 10
- Topics
- Bit manipulation, Graph, Binary search
- Solved
- No attempts yet
Problem
The city of Gridtopia is a matrix of square cells ("blocks") with rows and columns. Rows are numbered from 1 at the top to at the bottom, and columns are numbered from 1 at the left to at the right. The city is served by police stations. The -th station is in the block at row and column . No block contains more than one station.
Each station can only patrol blocks that are no more than blocks away from it, horizontally or vertically. The -th station can patrol the block at row and column if . Put another way, the -th station can patrol only the blocks inside the square of side length centered on that station.
You are the new police commissioner. Assign some blocks to exactly one station that can patrol them. Blocks that contain stations and blocks that no station can patrol are not assigned. Every other block must be assigned. Let be the number of blocks assigned to the -th station. Distribute the load as evenly as possible by minimizing the difference between the maximum and the minimum of all values. What is the smallest possible difference?
Input
The first line contains , the number of test cases. Each case begins with a line containing , , and . Then lines follow, each with , , and .
Output
For each test case, output one line Case #x: y, where is the test case number (starting from 1) and is the difference described above.
Limits
- .
- .
- for all .
- for all .
- For all , and/or . (No two stations are in the same block.)
- for all .
Hint
In Sample Case #1, the city is a grid with 3 rows and 4 columns, with one station in the upper left block and one station in the block to the left of the lower right block. The first station can only patrol the three blocks that touch the edge or corner of its block. Every other block is more than 1 block away horizontally or vertically. The second station can patrol any block in the grid except the blocks containing the stations. The difference in the number of assigned blocks is minimized if station 1 is assigned all three of the blocks it can patrol, and the remaining seven blocks are assigned to station 2.
In Sample Case #2, one optimal strategy is shown below. 1 is station 1, 2 is station 2, ! is a block assigned to station 1, @ is a block assigned to station 2, and . is a block assigned to neither station. A station's assigned blocks do not need to form a single continuous area.
@@@@.
!!!@.
!2!@.
1!!@.
!@!@.