PDP-36 (2024) - Camp common - 1 (meeting)

View as PDF

Submit solution

Points: 30 (partial)
Time limit: 1.0s
Memory limit: 64M

Author:
Problem type
Allowed languages
C, C++, Java, Pascal, Python

MEETING

Problem

A group of N programmers lives in a city where there are horizontal and vertical streets, equally spaced apart, forming a grid. The streets are numbered in order with natural numbers, both in the horizontal and vertical dimensions. Each programmer lives at an intersection in this city, and we know the coordinates of their homes.

The programmers want to meet at another programmer's house to work. They want to choose the house that minimizes the total distance all programmers will travel. Assume that each one starts from their own home (except for the "lucky" one where the meeting will be held at their house) and moves along the streets of the city, turning only at intersections. We consider that the distance between two neighboring intersections is equal to one unit.

Find the minimum distance that the programmers will need to cover if the meeting point is chosen optimally.

Input:

The first line of the input will contain an integer N, the number of programmers. Each of the next N lines of the input will contain two numbers X and Y, separated by a space. These are the coordinates of the house where the corresponding programmer lives. Assume that there will not be two programmers whose houses have the same coordinates.

Output:

The output should consist of exactly one line containing exactly one integer: the minimum total distance that the programmers will need to cover.

Constraints
  • 2 \le N \le 2.000.000.
  • The values of the coordinates will not be negative and will not exceed 10^7.
  • Time limit: 1 sec.
  • Memory limit: 64 MB.

  • For test cases with a total value of 37%, it will be N \le 10.000.

Note that for large values of N and the coordinates, the total distances (as well as some intermediate results that may be needed for their calculation) may exceed 2^{32}. Also, note that the size of the input data may be large. We recommend reading the data efficiently (see <cstdio> and scanf).

Example of Input - Output Files:

STDIN (incint.in)

7
1 3
3 2
3 5
6 9
10 1
12 4
5 7

STDOUT (incint.out)

39
Explanation of the example:

If the house of the third programmer, with coordinates (3,\;5), is chosen, the total distance that needs to be covered is 4 + 3 + 0 + 7 + 11 + 10 + 4 = 39. This is also the minimum possible.


Comments

There are no comments at the moment.