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.
Medium6SortingPrefix sumMathNo attempts yetTime limit5sMemory limit512 MBSin 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 (x1,y1) to (x2,y2) is ∣x1−x2∣+∣y1−y2∣.
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 (x1,y1,x2,y2) with x1≤x2 and y1≤y2. One person lives at every lattice point of the area, so the area (x1,y1,x2,y2) holds (x2−x1+1)×(y2−y1+1) 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.
The first line contains the number of test cases T. The first line of each test case contains the number of rectangular areas B. Each of the next B lines contains four integers x1, y1, x2, y2 separated by spaces, the coordinates of a rectangular area of people Sin invited.
For each test case, print one line in the form "Case #t: x y d", where t is the test case number starting from 1, (x,y) is the home that hosts the party, and d 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 x. If several still remain, pick the one with the smallest y among them.