#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:
- Each connector has at least one wire connected to a connector of the opposite color.
- 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)
- : an array of length , containing the positions of all red connectors in ascending order.
- : an array of length , 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:
- .
- (for all ).
- (for all ).
- The arrays and are already sorted in ascending order.
- All values in and are distinct.
Subtasks:
- ( points) .
- ( points) All red connector positions are smaller than any blue connector position.
- ( points) In every consecutive connectors, there is at least one red connector and one blue connector.
- ( points) All connectors have distinct positions in the range .
- ( 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 red connectors at positions , and .
- There are blue connectors at positions , and .
- The optimal total wire length is , so the subroutine should return .
- Note that there are two wires incident to the connector at position .
Translated by ChatGPT 5
京公网安备 11011102002149号