Meet and Party (Large)
Time limit5sMemory limit512 MB
Pick an invited home inside the given rectangles that minimizes the total Manhattan distance walked by all guests and report its coordinates and the sum.
- Level
Medium6 of 10
- Topics
- Sorting, Prefix sum, Math
- Solved
- No attempts yet
Problem
Sin lives in a city laid out as a grid. The city is a 2D plane, and people move only north, south, east or west along the grid lines. The distance from to is .
Sin wants to throw a house party this Sunday. The guest list is already settled, so all that is left is choosing whose home hosts the party.
Sin picked a few rectangular areas and invited everyone living inside them, and every invited person said yes. A rectangular area is given by four integers with and . One person lives at every lattice point of the area, so the area holds people.
The party has to be held at the home of one of the attendees. Sin wants to minimize the sum of the distances every attendee walks from home to the party. Find that home.
Input
The first line contains the number of test cases . The first line of each test case contains the number of rectangular areas . Each of the next lines contains four integers , , , separated by spaces, the coordinates of a rectangular area of people Sin invited.
Output
For each test case, print one line in the form "Case #t: x y d", where is the test case number starting from 1, is the home that hosts the party, and is the sum of the distances every attendee walks to that home.
If several homes give the same minimum sum, pick the one with the smallest . If several still remain, pick the one with the smallest among them.
Constraints
- ,
- The rectangular areas within one test case do not overlap.
- The total number of people in one test case is at least and at most .