AT_abc479_f.Narrow Passage

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are NN people standing on a number line. The people are numbered 1,2,…,N1, 2, \ldots, N, and initially person ii is at coordinate SiS_i. Here, the SiS_i are pairwise distinct.

You can repeatedly perform the following operation.

  • Choose one person. Let xx be the coordinate where that person currently is, and move them to x−1x - 1 or x+1x + 1. If there is a person at the destination, that person is simultaneously moved to coordinate xx.

Find the minimum number of operations required to reach a state where each person ii is at coordinate TiT_i. Here, the TiT_i are pairwise distinct.

数轴上有 NN 个人。这些人编号为 1,2,…,N1, 2, \ldots, N,初始时第 ii 个人位于坐标 SiS_i。其中,所有 SiS_i 两两互不相同。

你可以重复执行以下操作:

  • 选择一个人。设该人当前所在坐标为 xx,将其移动至 x−1x - 1 或 x+1x + 1。若目标位置上已有一人,则该人将同时被移至坐标 xx。

求达到如下状态所需的最少操作次数:每个人 ii 均位于坐标 TiT_i。其中,所有 TiT_i 两两互不相同。

输入格式

The input is given from Standard Input in the following format:

NN
S1S_1 S2S_2 …\ldots SNS_N
T1T_1 T2T_2 …\ldots TNT_N

输入从标准输入中按以下格式给出:

NN
S1S_1 S2S_2 …\ldots SNS_N
T1T_1 T2T_2 …\ldots TNT_N

输出格式

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,31, 2, 3 are at coordinates 4,2,54, 2, 5, respectively. By performing the operations as follows, persons 1,2,31, 2, 3 end up at coordinates 3,4,13, 4, 1, respectively, in six operations.

  • Move person 11 from coordinate 44 to 33. After this operation, persons 1,2,31, 2, 3 are at coordinates 3,2,53, 2, 5, respectively.

  • Move person 11 from coordinate 33 to 22. Since person 22 is at coordinate 22, person 22 simultaneously moves to coordinate 33. After this operation, persons 1,2,31, 2, 3 are at coordinates 2,3,52, 3, 5, respectively.

  • Move person 33 from coordinate 55 to 44. After this operation, persons 1,2,31, 2, 3 are at coordinates 2,3,42, 3, 4, respectively.

  • Move person 33 from coordinate 44 to 33. Since person 22 is at coordinate 33, person 22 simultaneously moves to coordinate 44. After this operation, persons 1,2,31, 2, 3 are at coordinates 2,4,32, 4, 3, respectively.

  • Move person 33 from coordinate 33 to 22. Since person 11 is at coordinate 22, person 11 simultaneously moves to coordinate 33. After this operation, persons 1,2,31, 2, 3 are at coordinates 3,4,23, 4, 2, respectively.

  • Move person 33 from coordinate 22 to 11. After this operation, persons 1,2,31, 2, 3 are at coordinates 3,4,13, 4, 1, respectively.

Constraints

  • 1≤N≤2×1051 \leq N \leq 2 \times 10^5
  • 0≤Si,Ti≤1090 \leq S_i, T_i \leq 10^9
  • Si≠SjS_i \neq S_j and Ti≠TjT_i \neq T_j if i≠ji \neq j.
  • All input values are integers.

样例 1 解释:
初始时,人员 1,2,31, 2, 3 分别位于坐标 4,2,54, 2, 5。通过执行如下六次操作,人员 1,2,31, 2, 3 最终分别到达坐标 3,4,13, 4, 1。

  • 将人员 11 从坐标 44 移动到 33。该操作后,人员 1,2,31, 2, 3 分别位于坐标 3,2,53, 2, 5。
  • 将人员 11 从坐标 33 移动到 22。由于人员 22 当前位于坐标 22,因此人员 22 同时移动至坐标 33。该操作后,人员 1,2,31, 2, 3 分别位于坐标 2,3,52, 3, 5。
  • 将人员 33 从坐标 55 移动到 44。该操作后,人员 1,2,31, 2, 3 分别位于坐标 2,3,42, 3, 4。
  • 将人员 33 从坐标 44 移动到 33。由于人员 22 当前位于坐标 33,因此人员 22 同时移动至坐标 44。该操作后,人员 1,2,31, 2, 3 分别位于坐标 2,4,32, 4, 3。
  • 将人员 33 从坐标 33 移动到 22。由于人员 11 当前位于坐标 22,因此人员 11 同时移动至坐标 33。该操作后,人员 1,2,31, 2, 3 分别位于坐标 3,4,23, 4, 2。
  • 将人员 33 从坐标 22 移动到 11。该操作后,人员 1,2,31, 2, 3 分别位于坐标 3,4,13, 4, 1。

约束条件

  • 1≤N≤2×1051 \leq N \leq 2 \times 10^5
  • 0≤Si,Ti≤1090 \leq S_i, T_i \leq 10^9
  • 若 i≠ji \neq j,则 Si≠SjS_i \neq S_j 且 Ti≠TjT_i \neq T_j。
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页