AT_abc479_f.Narrow Passage
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:1024MB
AC君温馨提醒
该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There are N people standing on a number line. The people are numbered 1,2,…,N, and initially person i is at coordinate Si. Here, the Si are pairwise distinct.
You can repeatedly perform the following operation.
- Choose one person. Let x be the coordinate where that person currently is, and move them to x−1 or x+1. If there is a person at the destination, that person is simultaneously moved to coordinate x.
Find the minimum number of operations required to reach a state where each person i is at coordinate Ti. Here, the Ti are pairwise distinct.
数轴上有 N 个人。这些人编号为 1,2,…,N,初始时第 i 个人位于坐标 Si。其中,所有 Si 两两互不相同。
你可以重复执行以下操作:
- 选择一个人。设该人当前所在坐标为 x,将其移动至 x−1 或 x+1。若目标位置上已有一人,则该人将同时被移至坐标 x。
求达到如下状态所需的最少操作次数:每个人 i 均位于坐标 Ti。其中,所有 Ti 两两互不相同。
输入格式
The input is given from Standard Input in the following format:
N
S1 S2 … SN
T1 T2 … TN
输入从标准输入中按以下格式给出:
N
S1 S2 … SN
T1 T2 … TN
输出格式
Output the answer.
输出答案。
输入输出样例
输入#1
3 4 2 5 3 4 1
输出#1
6
输入#2
4 2 4 5 9 2 4 5 1
输出#2
11
输入#3
6 5 2 9 1 6 8 1 4 7 0 9 3
输出#3
13
说明/提示
Sample 1 Explanation:
Initially, persons 1,2,3 are at coordinates 4,2,5, respectively. By performing the operations as follows, persons 1,2,3 end up at coordinates 3,4,1, respectively, in six operations.
-
Move person 1 from coordinate 4 to 3. After this operation, persons 1,2,3 are at coordinates 3,2,5, respectively.
-
Move person 1 from coordinate 3 to 2. Since person 2 is at coordinate 2, person 2 simultaneously moves to coordinate 3. After this operation, persons 1,2,3 are at coordinates 2,3,5, respectively.
-
Move person 3 from coordinate 5 to 4. After this operation, persons 1,2,3 are at coordinates 2,3,4, respectively.
-
Move person 3 from coordinate 4 to 3. Since person 2 is at coordinate 3, person 2 simultaneously moves to coordinate 4. After this operation, persons 1,2,3 are at coordinates 2,4,3, respectively.
-
Move person 3 from coordinate 3 to 2. Since person 1 is at coordinate 2, person 1 simultaneously moves to coordinate 3. After this operation, persons 1,2,3 are at coordinates 3,4,2, respectively.
-
Move person 3 from coordinate 2 to 1. After this operation, persons 1,2,3 are at coordinates 3,4,1, respectively.
Constraints
- 1≤N≤2×105
- 0≤Si,Ti≤109
- Si=Sj and Ti=Tj if i=j.
- All input values are integers.
样例 1 解释:
初始时,人员 1,2,3 分别位于坐标 4,2,5。通过执行如下六次操作,人员 1,2,3 最终分别到达坐标 3,4,1。
- 将人员 1 从坐标 4 移动到 3。该操作后,人员 1,2,3 分别位于坐标 3,2,5。
- 将人员 1 从坐标 3 移动到 2。由于人员 2 当前位于坐标 2,因此人员 2 同时移动至坐标 3。该操作后,人员 1,2,3 分别位于坐标 2,3,5。
- 将人员 3 从坐标 5 移动到 4。该操作后,人员 1,2,3 分别位于坐标 2,3,4。
- 将人员 3 从坐标 4 移动到 3。由于人员 2 当前位于坐标 3,因此人员 2 同时移动至坐标 4。该操作后,人员 1,2,3 分别位于坐标 2,4,3。
- 将人员 3 从坐标 3 移动到 2。由于人员 1 当前位于坐标 2,因此人员 1 同时移动至坐标 3。该操作后,人员 1,2,3 分别位于坐标 3,4,2。
- 将人员 3 从坐标 2 移动到 1。该操作后,人员 1,2,3 分别位于坐标 3,4,1。
约束条件
- 1≤N≤2×105
- 0≤Si,Ti≤109
- 若 i=j,则 Si=Sj 且 Ti=Tj。
- 所有输入值均为整数。
输入解题思路,AI测评打分。不知道怎么写?