Shawarma Tent (Codeforces 1271C)
Tags: Greedy, geometry

The map of the capital of Berland can be viewed on the infinite coordinate plane. Each point with integer coordinates contains a building, and there are streets connecting every building to four neighbouring buildings. All streets are parallel to the coordinate axes.The main school of the capital is located in $$$(s_x, s_y)$$$. There are $$$n$$$ students attending this school, the $$$i$$$-th of them lives in the house located in $$$(x_i, y_i)$$$. It is possible that some students live in the same house, but no student lives in $$$(s_x, s_y)$$$.After classes end, each student walks from the school to his house along one of the shortest paths. So the distance the $$$i$$$-th student goes from the school to his house is $$$|s_x - x_i| + |s_y - y_i|$$$.The Provision Department of Berland has decided to open a shawarma tent somewhere in the capital (at some point with integer coordinates). It is considered that the $$$i$$$-th student will buy a shawarma if at least one of the shortest paths from the school to the $$$i$$$-th student's house goes through the point where the shawarma tent is located. It is forbidden to place the shawarma tent at the point where the school is located, but the coordinates of the shawarma tent may coincide with the coordinates of the house of some student (or even multiple students).You want to find the maximum possible number of students buying shawarma and the optimal location for the tent itself.

Input: The first line contains three integers $$$n$$$, $$$s_x$$$, $$$s_y$$$ ($$$1 \le n \le 200\,000$$$, $$$0 \le s_x, s_y \le 10^{9}$$$) — the number of students and the coordinates of the school, respectively.Then $$$n$$$ lines follow. The $$$i$$$-th of them contains two integers $$$x_i$$$, $$$y_i$$$ ($$$0 \le x_i, y_i \le 10^{9}$$$) — the location of the house where the $$$i$$$-th student lives. Some locations of houses may coincide, but no student lives in the same location where the school is situated.

Output: The output should consist of two lines. The first of them should contain one integer $$$c$$$ — the maximum number of students that will buy shawarmas at the tent. The second line should contain two integers $$$p_x$$$ and $$$p_y$$$ — the coordinates where the tent should be located. If there are multiple answers, print any of them. Note that each of $$$p_x$$$ and $$$p_y$$$ should be not less than $$$0$$$ and not greater than $$$10^{9}$$$.

Note: In the first example, If we build the shawarma tent in $$$(4, 2)$$$, then the students living in $$$(4, 2)$$$, $$$(4, 1)$$$ and $$$(5, 1)$$$ will visit it.In the second example, it is possible to build the shawarma tent in $$$(1, 1)$$$, then both students living in $$$(0, 0)$$$ will visit it.

Sample Inputs:
4 3 2
1 3
4 2
5 1
4 1

3 100 100
0 0
0 0
100 200

7 10 12
5 6
20 23
15 4
16 5
4 54
12 1
4 15

Sample Outputs:
3
4 2

2
99 100

4
10 11

Solution (Python):
import sys
input = sys.stdin.readline
(n, x, y) = map(int, input().split())
(l, r, u, d) = (0, 0, 0, 0)
for _ in range(n):
    (j, k) = map(int, input().split())
    if j < x:
        l += 1
    elif j > x:
        r += 1
    if k > y:
        u += 1
    elif k < y:
        d += 1
mx = max(l, r, u, d)
if l == mx:
    x -= 1
elif r == mx:
    x += 1
elif u == mx:
    y += 1
else:
    y -= 1
print(mx)
print(x, y)

Solution (Python):
(n, x, y) = map(int, input().split())
(l, r, u, d) = (0, 0, 0, 0)
for _ in range(n):
    (j, k) = map(int, input().split())
    if j < x:
        l += 1
    elif j > x:
        r += 1
    if k > y:
        u += 1
    elif k < y:
        d += 1
mx = max(l, r, u, d)
if l == mx:
    x -= 1
elif r == mx:
    x += 1
elif u == mx:
    y += 1
else:
    y -= 1
print(mx)
print(x, y)

Solution (Python):
import sys

def main():
    input = sys.stdin.read()
    data = list(map(int, input.split()))
    (n, x, y) = data[0:3]
    idx = [0] * 5
    for i in range(3, len(data), 2):
        if data[i] > x:
            idx[1] += 1
        elif data[i] < x:
            idx[2] += 1
        if data[i + 1] > y:
            idx[3] += 1
        elif data[i + 1] < y:
            idx[4] += 1
    nt = 0
    ans = 0
    for i in range(1, 5):
        if idx[i] > ans:
            ans = idx[i]
            nt = i
    print(ans)
    if nt == 1:
        print(str(x + 1) + ' ' + str(y))
    elif nt == 2:
        print(str(x - 1) + ' ' + str(y))
    elif nt == 3:
        print(str(x) + ' ' + str(y + 1))
    else:
        print(str(x) + ' ' + str(y - 1))
if __name__ == '__main__':
    main()