Coffee Shops
Time limit5sMemory limit128 MB
For each query radius m, find the grid intersection reached by the most coffee shops within Manhattan distance m, breaking ties by smallest y then smallest x.
- Level
Hard8 of 10
- Topics
- Prefix sum, Geometry, Brute force, Implementation
- Solved
- No attempts yet
Problem
A neighborhood has many coffee shops. The neighborhood is a square grid: every street runs north-south or east-west, so the intersections form lattice points.
The distance between two intersections and is the Manhattan distance , which equals the number of blocks you must walk to go from one to the other.
Given the positions of all coffee shops and a walking limit of blocks, write a program that finds the intersection from which the number of coffee shops within blocks is as large as possible. A candidate intersection is any lattice point inside the city, i.e. an with and .
Input
The input consists of several test cases. Each test case describes one city.
The first line of each test case contains four integers , , , and . The city has size (), it contains coffee shops (), and there are queries ().
Each of the next lines contains two integers and (, ), the position of the -th coffee shop. At most one coffee shop sits at any intersection.
Each of the next lines contains one integer (), the maximum number of blocks that may be walked.
The last line of the input contains four zeros, which marks the end of the input.
Output
For each test case, first print the test case number in the form Case k: (where starts at ). Then, for each query, print one line: for that query's , print the maximum number of coffee shops that lie within blocks of some intersection, followed by the position of an intersection that achieves that maximum, in the form count (x,y).
If several intersections achieve the maximum, print the southernmost one (the smallest ); if there is still a tie, print the westernmost one (the smallest ).