CF343C.Read Time
普及+/提高
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Mad scientist Mike does not use slow hard disks. His modification of a hard drive has not one, but n different heads that can read data in parallel.
When viewed from the side, Mike's hard drive is an endless array of tracks. The tracks of the array are numbered from left to right with integers, starting with 1. In the initial state the i-th reading head is above the track number h__i. For each of the reading heads, the hard drive's firmware can move the head exactly one track to the right or to the left, or leave it on the current track. During the operation each head's movement does not affect the movement of the other heads: the heads can change their relative order; there can be multiple reading heads above any of the tracks. A track is considered read if at least one head has visited this track. In particular, all of the tracks numbered _h_1, _h_2, ..., h__n have been read at the beginning of the operation.

Mike needs to read the data on m distinct tracks with numbers _p_1, _p_2, ..., p__m. Determine the minimum time the hard drive firmware needs to move the heads and read all the given tracks. Note that an arbitrary number of other tracks can also be read.
疯狂科学家迈克不使用速度缓慢的硬盘。他对硬盘所做的改进,不是只配备一个读取磁头,而是配备了 n 个可并行读取数据的不同磁头。
从侧面观察,迈克的硬盘是一个无限延伸的磁道阵列。该阵列中的磁道从左到右依次用整数编号,起始编号为 1。在初始状态下,第 i 个读取磁头位于编号为 hi 的磁道正上方。对于每个读取磁头,硬盘固件均可将其恰好向右或向左移动一个磁道,或保持其停留在当前磁道。在操作过程中,各磁头的移动互不影响:磁头之间的相对顺序可能发生变化;任意一个磁道上都可能有多个读取磁头同时存在。若至少有一个磁头曾访问过某磁道,则该磁道即被视为“已被读取”。特别地,在操作开始时,所有编号为 h1, h2, ..., hn 的磁道均已处于“已被读取”状态。

迈克需要读取 m 个编号分别为 p1, p2, ..., pm 的不同磁道上的数据。请确定硬盘固件完成全部指定磁道读取所需的最短时间。注意:在此过程中,可以额外读取任意数量的其他磁道。
输入格式
The first line of the input contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of disk heads and the number of tracks to read, accordingly. The second line contains n distinct integers h__i in ascending order (1 ≤ h__i ≤ 1010, h__i < h__i + 1) — the initial positions of the heads. The third line contains m distinct integers p__i in ascending order (1 ≤ p__i ≤ 1010, p__i < p__i + 1) - the numbers of tracks to read.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams or the %I64d specifier.
输入的第一行包含两个以空格分隔的整数 n、m(1 ≤ n, m ≤ 105),分别表示磁盘磁头的数量和待读取的磁道数量。
第二行包含 n 个严格递增的互异整数 hi(1 ≤ hi ≤ 1010,且 hi < hi+1),表示各磁头的初始位置。
第三行包含 m 个严格递增的互异整数 pi(1 ≤ pi ≤ 1010,且 pi < pi+1),表示待读取的磁道编号。
请注意:在 C++ 中,请勿使用 %lld 说明符读写 64 位整数。推荐使用 cin/cout 流,或使用 %I64d 说明符。
输出格式
Print a single number — the minimum time required, in seconds, to read all the needed tracks.
输出一个整数——读取所有所需音轨所需的最短时间(单位:秒)。
输入输出样例
输入#1
3 4 2 5 6 1 3 6 8
输出#1
2
输入#2
3 3 1 2 3 1 2 3
输出#2
0
输入#3
1 2 165 142 200
输出#3
81
说明/提示
The first test coincides with the figure. In this case the given tracks can be read in 2 seconds in the following way:
- during the first second move the 1-st head to the left and let it stay there;
- move the second head to the left twice;
- move the third head to the right twice (note that the 6-th track has already been read at the beginning).
One cannot read the tracks in 1 second as the 3-rd head is at distance 2 from the 8-th track.
第一个测试用例与图示一致。此时,给定的磁道可以在 2 秒内按如下方式读取:
- 第一秒内,将第 1 个磁头向左移动,并使其停留在该位置;
- 将第 2 个磁头向左移动两次;
- 将第 3 个磁头向右移动两次(注意:第 6 号磁道在初始时刻已被读取)。
无法在 1 秒内读取所有磁道,因为第 3 个磁头距离第 8 号磁道的距离为 2。
输入解题思路,AI测评打分。不知道怎么写?