Gathering
Time limit3sMemory limit256 MB
Pick an integer intersection within Manhattan distance d of every house so the sum of Manhattan travel distances is smallest, or report impossible.
- Level
Medium7 of 10
- Topics
- Geometry, Sorting, Prefix sum, Binary search
- Solved
- No attempts yet
Problem
The citizens of Fictitia have had enough. The city keeps growing, and it keeps getting more boring. Fictitia has horizontal and vertical streets only, and the gap between two neighbouring parallel streets is always the same. Take that gap as distance .
To make their unhappiness known to the city council, a group of citizens agreed to gather at one intersection and protest. The question is which intersection. The intersections are much alike, so someone proposed picking the intersection that minimises the total distance everyone has to travel. Every citizen lives next to an intersection, so a citizen living at travels .
This could be a problem for the citizens who live far away, because they might not get there in time. The group therefore decided that the chosen intersection has to be at most a distance away from every citizen. Under that restriction, find an intersection that minimises the total distance everyone has to travel.
Input
- The first line has one integer (), the number of citizens.
- Each of the next lines has two integers and (), the coordinates of one citizen's house.
- The last line has one integer (), the maximum distance each citizen should have to travel.
Several citizens can live at the same intersection.
Output
Print one line with a single integer, the smallest possible total distance that all citizens have to travel. If no intersection lies within distance of every citizen, print impossible instead.