Under construction

Still being built. Things may change

Board Meeting

Time limit60sMemory limit1024 MB

Summary
Find a meeting cell that minimizes the total Chebyshev distance to N kings whose number and coordinates are unknown to you.
Level

Medium6 of 10

Topics
Math, Binary search, Implementation
Solved
No attempts yet

Problem

Note that it is not necessary to know anything about the rules of chess to solve this problem.

There are NN kings on an infinite chessboard (a two-dimensional grid), located in cells with coordinates (X1,Y1),(X2,Y2),…,(XN,YN)(X_1, Y_1), (X_2, Y_2), \ldots, (X_N, Y_N). Neither NN nor the kings' coordinates are known to you. You do know the following:

  • NN is at least 1 and at most Nmax⁡N_{\max}.
  • No king's coordinates (XX or YY) have an absolute value exceeding MM.
  • The NN kings occupy NN different cells.

The kings want to meet in a single cell of the board. If cell (X,Y)(X, Y) is chosen as the meeting cell, the ii-th king needs max⁡(∣X−Xi∣,∣Y−Yi∣)\max(|X-X_i|, |Y-Y_i|) moves to get there. The total number of moves is the sum of these values over all ii. The exact path each king takes does not matter. Only the start and end cells matter, and the move count can always be computed with the formula above.

Constraints

  • 1≤T≤151 \le T \le 15.
  • M=106M = 10^6.
  • −M≤Xi≤M-M \le X_i \le M for all ii.
  • −M≤Yi≤M-M \le Y_i \le M for all ii.
  • The pairs (Xi,Yi)(X_i, Y_i) are distinct.
  • −10×M≤Ci≤10×M-10 \times M \le C_i \le 10 \times M for all ii.
  • −10×M≤Di≤10×M-10 \times M \le D_i \le 10 \times M for all ii.
  • R=1000R = 1000.

Examples1

  1. Example 1

    Input
    1
    0 0
    1
    0 0
    
    Expected output
    0