#P16270. [蓝桥杯 2026 省 Java B 组] 共享单车

    ID: 18290 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 显示难度普及 上传者: 标签>动态规划 DP2026蓝桥杯省赛

[蓝桥杯 2026 省 Java B 组] 共享单车

Problem Description

Xiao Lan’s job is to manage shared bicycles. Now, he needs to move nn shared bicycles that are not properly parked to mm parking spots.

Xiao Lan’s management area is a street, which can be viewed as a number line. The ii-th bicycle is at position aia_i, and the jj-th parking spot is at position bjb_j.

Moving a bicycle from position xx to a parking spot at position yy costs xy|x - y| units of effort.

Each parking spot can hold at most one bicycle. It is known that nmn \leq m, 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 n,mn, m, representing the number of bicycles and the number of parking spots.

The second line contains nn positive integers a1,a2,,ana_1, a_2, \dots, a_n, representing the positions of the bicycles.

The third line contains mm positive integers b1,b2,,bmb_1, b_2, \dots, b_m, 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 11 to the parking spot at position 22.
  • Move the bicycle at position 33 to the parking spot at position 44.
  • Move the bicycle at position 77 to the parking spot at position 88.

The total cost is:

$$\begin{aligned} |1 - 2| + |3 - 4| + |7 - 8| = 1 + 1 + 1 = 3 \end{aligned}$$

Therefore, the minimum effort required is 33.

Constraints and Notes for Test Cases

For 40%40\% of the test cases, n,m8n, m \leq 8.

For another 20%20\% of the test cases, n=mn = m.

For all test cases, 1nm50001 \leq n \leq m \leq 5000, 1ai,bi1091 \leq a_i, b_i \leq 10^9.

Translated by ChatGPT 5