Contents
Staying hydrated solution kickstart

For Solution
Click Here!
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 like the study desk, bed and coffee table among other places.
The room is represented in the form of a coordinate plane. The number of steps Grace needs to go from Point A to Point B is equal to the Manhatten distance between the 2 points. This means, Grace can only walk parallel to the axes of the coordinate plane and with each step, she can move one unit in either 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 will be minimum?
Notes:
Staying hydrated solution kickstart
 All the furniture (like study desk, bed, or coffee table) can be represented as rectangles of nonzero area in the plane with edges parallel to the axes.
 It is possible for furniture pieces to overlap, as she likes to work on her bedtable too.
 Assume that Grace can simply pass through the furniture while walking and does not need to go around them.
Input
Staying hydrated solution kickstart
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 of them describing one object. The iith line contains four integers, xi,1xi,1, yi,1yi,1, xi,2xi,2, yi,2yi,2, where (xi,1xi,1, yi,1yi,1) represents coordinates of the bottom left corner and (xi,2xi,2, yi,2yi,2) represents coordinates of the top right corner of the iith rectangular object.
Output
Staying hydrated solution kickstart
For each test case, output one line containing Case #ii: xx yy
, 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 will be minimum.
Note, the bottle can lie on the floor or on top of any furniture but should be placed on integer coordinates only.
If multiple solutions exist, output the one with minimum x coordinate, if multiple solutions have the same x coordinate output the one with minimum y coordinate.
Limits
Staying hydrated solution kickstart
Memory limit: 1 GB.
1≤T≤1001≤T≤100.
xi,1<xi,2xi,1<xi,2, for all ii.
yi,1<yi,2yi,1<yi,2, for all ii.
Test Set 1
Staying hydrated solution kickstart
Time limit: 40 seconds.
1≤K≤201≤K≤20.
−100≤xi,1,xi,2,yi,1,yi,2≤100−100≤xi,1,xi,2,yi,1,yi,2≤100, for all ii.
Test Set 2
Staying hydrated solution kickstart
Time limit: 90 seconds.
1≤K≤1051≤K≤105
−109≤xi,1,xi,2,yi,1,yi,2≤109−109≤xi,1,xi,2,yi,1,yi,2≤109, for all ii.
Sample
Staying hydrated solution kickstart
2 3 0 0 1 1 2 3 4 6 0 3 5 9 1 0 0 1 1
Case #1: 1 3 Case #2: 0 0
Staying hydrated solution kickstart
In Sample Case #1, Grace can place the bottle at coordinates (11, 33). It is at a distance of 22 steps from first object, 11 step from second and 00 steps from the third one which gives us 33 as the minimum possible sum of steps from a point.
In Sample Case #2, the water bottle can lie anywhere on the object itself but coordinates (00, 00) correspond to the minimum x and y coordinates.

For Solution
Click Here!