#P16270. [蓝桥杯 2026 省 Java B 组] 共享单车
[蓝桥杯 2026 省 Java B 组] 共享单车
Problem Description
Xiao Lan’s job is to manage shared bicycles. Now, he needs to move shared bicycles that are not properly parked to parking spots.
Xiao Lan’s management area is a street, which can be viewed as a number line. The -th bicycle is at position , and the -th parking spot is at position .
Moving a bicycle from position to a parking spot at position costs units of effort.
Each parking spot can hold at most one bicycle. It is known that , so it is always possible to assign a parking spot to every bicycle. You need to compute: under a reasonable assignment of bicycles to parking spots, what is the minimum total effort Xiao Lan needs.
Input Format
The input consists of 3 lines.
The first line contains two positive integers , representing the number of bicycles and the number of parking spots.
The second line contains positive integers , representing the positions of the bicycles.
The third line contains positive integers , representing the positions of the parking spots.
Output Format
Output one line with one positive integer, representing the minimum effort Xiao Lan needs to spend.
3 4
1 3 7
2 4 5 8
3
3 4
3 1 3
5 2 2 8
4
Hint
Sample Explanation 1
One optimal assignment is as follows:
- Move the bicycle at position to the parking spot at position .
- Move the bicycle at position to the parking spot at position .
- Move the bicycle at position to the parking spot at position .
The total cost is:
$$\begin{aligned} |1 - 2| + |3 - 4| + |7 - 8| = 1 + 1 + 1 = 3 \end{aligned}$$Therefore, the minimum effort required is .
Constraints and Notes for Test Cases
For of the test cases, .
For another of the test cases, .
For all test cases, , .
Translated by ChatGPT 5