Meet and party (small)
InterviewTime limit5sMemory limit512 MB
Choose an attendee home inside the given rectangles that minimizes the total Manhattan travel distance, breaking ties by smaller x then y.
- Level
Easy3 of 10
- Topics
- Brute force
- Solved
- No attempts yet
Problem
Little Sin lives in a city laid out as a Manhattan grid. The city is a 2D plane, and people move only north, west, south or east along the grid lines. The distance from to is .
Little Sin loves parties and wants to host one at a house this Sunday. She already has the guest list, so all that is left is choosing whose home hosts the party.
Little Sin invited everyone living in a few rectangular areas, and every invited person said yes. A rectangular area is written as with and . One person lives at every integer point inside the area, so the area holds people.
Little Sin knows the coordinates of each area. The party has to be at the home of one of the attendees, and she does not want the others to travel far, so she wants to minimize the sum of the distances from every attendee's home to the party. Help her find that home.
Input
The first line contains the number of test cases .
Each test case starts with a line containing the number of rectangular areas . The next lines each contain four integers , , , separated by spaces, giving the coordinates of one area.
Output
For each test case, print one line in the format Case #t: x y d.
Here is the test case number starting from 1, and is the home that hosts the party. If several homes reach the same minimum total distance, choose the one with the smallest ; if several remain, choose the one among them with the smallest . The value is the sum of the distances from every attendee's home to .
Constraints
- ,
- The rectangular areas within one test case do not overlap.
- The total number of people in one test case is at least 1 and at most 1000.