Hiding Nuts

Interview

Time limit1sMemory limit512 MB

Summary
Given N grid points, pick the one minimizing the sum of Manhattan distances to all other points, breaking ties by smallest X then smallest Y.
Level

Medium4 of 10

Topics
Math, Brute force, Geometry, Sorting
Solved
No attempts yet

Problem

Bob, a squirrel, has set up N hiding places throughout his territory, where he stores nuts for the winter. The hiding places sit at integer coordinates on a grid. He would like to choose one of those hiding places as his main den. He is very worried about other animals eating his nuts. He would therefore like to choose a den that minimizes the average distance of the paths between the den and the N − 1 remaining hiding places.

Bob has a poor sense of direction. In order not to get lost between his den and each hiding place, he decides that he will only travel along the horizontal and vertical lines of the grid at integer coordinates.

For instance, the distance between points D and E in the following grid is 4 (one path of minimal length between D and E is drawn in red below), and the average distance between D and the other points is 13/5.

Input

The input consists of the following lines.

  • The first line contains the total number N of hiding places, an integer.
  • The next N lines contain Xi and Yi, the integer coordinates of the i-th hiding place, separated by a space.

Output

Print the coordinates of a hiding place that minimizes the distance to the other hiding places. If there is a tie, that hiding place must be the one with the smallest X coordinate, and if there is still a tie, the one with the smallest Y coordinate.

Constraints

  • 1 ≤ N ≤ 1 000
  • 0 ≤ Xi, Yi ≤ 1 000 000 for all points

No two hiding places are at the same coordinates.

Examples1

  1. Example 1

    Input
    6
    2 3
    4 3
    1 1
    3 1
    0 0
    3 2
    
    Expected output
    3 1