This page is still under construction.

Parts of this page are still being built. What you see may change.

Staying Hydrated

Interview

Memory limit1024 MB

Summary
Choose integer coordinates minimizing the sum of Manhattan distances to K axis-aligned rectangles, breaking ties by smallest x then smallest y.
Level

Medium6 of 10

Topics
Math, Greedy, Sorting, Geometry
Solved
No attempts yet

Problem

With online classes in full swing, it is important for Grace to take breaks and keep herself hydrated at all times. She has decided to place a water bottle in her room in the most convenient place. This means that the position of this water bottle should be close to all the places in the room where she generally hangs out, such as the study desk, bed, and coffee table, among other places.

The room is represented as a coordinate plane. The number of steps Grace needs to go from Point A to Point B equals the Manhattan distance between the two points. That is, Grace can walk only parallel to the axes of the coordinate plane, and with each step she can move one unit in any of the four directions.

Can you help her find a position in the room to keep the bottle such that the sum of steps from the bottle to all her favourite furniture pieces is minimum?

Notes:

  • All the furniture (such as the study desk, bed, or coffee table) can be represented as rectangles of non-zero area in the plane with edges parallel to the axes.
  • Furniture pieces can overlap, since she likes to work on her bed-table too.
  • Assume that Grace can pass through the furniture while walking and does not need to go around it.

Input

The first line of the input gives the number of test cases, TT. TT test cases follow.

The first line of each test case contains an integer KK, which represents the number of objects in Grace's room.

KK lines follow, each describing one object. The ii-th line contains four integers, xi,1x_{i,1}, yi,1y_{i,1}, xi,2x_{i,2}, yi,2y_{i,2}, where (xi,1x_{i,1}, yi,1y_{i,1}) is the coordinate of the bottom left corner and (xi,2x_{i,2}, yi,2y_{i,2}) is the coordinate of the top right corner of the ii-th rectangular object.

Output

For each test case, output one line containing Case #i: x y, where ii is the test case number (starting from 1) and xx and yy are coordinates of the water bottle such that the sum of steps from these coordinates to all the furniture pieces is minimum.

The bottle can lie on the floor or on top of any furniture, but it must be placed on integer coordinates only.

If multiple solutions exist, output the one with minimum xx coordinate; if multiple solutions have the same xx coordinate, output the one with minimum yy coordinate.

Constraints

  • 1≤T≤1001 \le T \le 100.
  • xi,1<xi,2x_{i,1} < x_{i,2}, for all ii.
  • yi,1<yi,2y_{i,1} < y_{i,2}, for all ii.

Examples1

  1. Example 1

    Input
    2
    3
    0 0 1 1
    2 3 4 6
    0 3 5 9
    1
    0 0 1 1
    
    Expected output
    Case #1: 1 3
    Case #2: 0 0