#P3837. [IOI 2017] Wiring

[IOI 2017] Wiring

Description

Maryam is an electrical engineer. She is designing a wiring plan for a communication tower. There are connectors placed at various heights on this tower. A wire can be used to connect any two connectors. Each connector may be connected to any number of wires. There are two types of connectors: red connectors and blue connectors.

For convenience, the tower is regarded as a straight line, and the red and blue connectors are located at some non-negative integer coordinates on this line. The length of a wire is the distance between the two connectors it connects.

Your task is to help Maryam find a wiring plan that satisfies the following:

  1. Each connector has at least one wire connected to a connector of the opposite color.
  2. The total length of the wires is minimized.

Implementation details:

You need to implement the following subroutine:

long long min_total_length(std::vector<int> r, std::vector<int> b)

  • rr: an array of length nn, containing the positions of all red connectors in ascending order.
  • bb: an array of length mm, containing the positions of all blue connectors in ascending order.
  • This subroutine should return the minimal total length of wires among all valid wirings.
  • Note that the return type of this subroutine is long long.

Constraints:

  • 1n,m1000001 \leqslant n,m \leqslant 100000.
  • 0r[i]1090 \leqslant r[i] \leqslant 10^9 (for all 0in10 \leqslant i \leqslant n-1).
  • 0b[i]1090 \leqslant b[i] \leqslant 10^9 (for all 0im10 \leqslant i \leqslant m-1).
  • The arrays rr and bb are already sorted in ascending order.
  • All n+mn+m values in rr and bb are distinct.

Subtasks:

  1. (77 points) n,m200n,m\leqslant 200.
  2. (1313 points) All red connector positions are smaller than any blue connector position.
  3. (1010 points) In every 77 consecutive connectors, there is at least one red connector and one blue connector.
  4. (2525 points) All connectors have distinct positions in the range [1,n+m][1,n+m].
  5. (4545 points) No additional constraints.

Input Format

You need to implement the subroutine described above.

Output Format

Return a long long value.

r = [1, 2, 3, 7]
b = [0, 4, 5, 9, 10]
10

Hint

Sample function call:

min_total_length([1, 2, 3, 7], [0, 4, 5, 9, 10])

The following figure illustrates the sample data.

  • The tower is drawn horizontally.
  • Because the statement is printed in black and white, red connectors are drawn darker, and blue connectors lighter.
  • There are 44 red connectors at positions 1,2,31, 2, 3, and 77.
  • There are 55 blue connectors at positions 0,4,5,90, 4, 5, 9, and 1010.
  • The optimal total wire length is 1+2+2+2+3=101+2+2+2+3=10, so the subroutine should return 1010.
  • Note that there are two wires incident to the connector at position 77.

Translated by ChatGPT 5