English · Original
Chinese · Translation
Formal · Original
There are _n_ people and _k_ keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all _n_ people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
## Input
The first line contains three integers _n_, _k_ and _p_ (1 ≤ _n_ ≤ 1 000, _n_ ≤ _k_ ≤ 2 000, 1 ≤ _p_ ≤ 109) — the number of people, the number of keys and the office location.
The second line contains _n_ distinct integers _a_1, _a_2, ..., _a__n_ (1 ≤ _a__i_ ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains _k_ distinct integers _b_1, _b_2, ..., _b__k_ (1 ≤ _b__j_ ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
## Output
Print the minimum time (in seconds) needed for all _n_ to reach the office with keys.
[samples]
## Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
一条直线上有 #cf_span[n] 个人和 #cf_span[k] 把钥匙。每个人都要到达同样位于这条直线上的办公室。为此,他需要先到达某个有钥匙的点,拿起钥匙,然后前往办公室。一旦某把钥匙被某人取走,其他人就无法再取走它。
你需要确定所有人拿到钥匙并到达办公室所需的最少时间。假设每个人每秒移动一单位距离。如果两个人同时到达同一把钥匙,只有其中一人能取走它。一个人可以经过有钥匙的点而不取走它。
第一行包含三个整数 #cf_span[n], #cf_span[k] 和 #cf_span[p](#cf_span[1 ≤ n ≤ 1 000], #cf_span[n ≤ k ≤ 2 000], #cf_span[1 ≤ p ≤ 109])——分别表示人数、钥匙数和办公室位置。
第二行包含 #cf_span[n] 个互不相同的整数 #cf_span[a1, a2, ..., an](#cf_span[1 ≤ ai ≤ 109])——表示每个人初始的位置。位置顺序任意。
第三行包含 #cf_span[k] 个互不相同的整数 #cf_span[b1, b2, ..., bk](#cf_span[1 ≤ bj ≤ 109])——表示钥匙的位置。位置顺序任意。
注意:同一个点上不会有多于一个人或一把钥匙。一个人和一把钥匙可以位于同一点。
请输出所有人拿到钥匙并到达办公室所需的最少时间(以秒为单位)。
在第一个例子中,位于点 #cf_span[20] 的人应取走位于点 #cf_span[40] 的钥匙,然后前往位于点 #cf_span[50] 的办公室,耗时 #cf_span[30] 秒。位于点 #cf_span[100] 的人可以取走位于点 #cf_span[80] 的钥匙并前往办公室,耗时 #cf_span[50] 秒。因此,经过 #cf_span[50] 秒后,所有人都带着钥匙到达了办公室。
## Input
第一行包含三个整数 #cf_span[n], #cf_span[k] 和 #cf_span[p](#cf_span[1 ≤ n ≤ 1 000], #cf_span[n ≤ k ≤ 2 000], #cf_span[1 ≤ p ≤ 109])——分别表示人数、钥匙数和办公室位置。第二行包含 #cf_span[n] 个互不相同的整数 #cf_span[a1, a2, ..., an](#cf_span[1 ≤ ai ≤ 109])——表示每个人初始的位置。位置顺序任意。第三行包含 #cf_span[k] 个互不相同的整数 #cf_span[b1, b2, ..., bk](#cf_span[1 ≤ bj ≤ 109])——表示钥匙的位置。位置顺序任意。注意:同一个点上不会有多于一个人或一把钥匙。一个人和一把钥匙可以位于同一点。
## Output
请输出所有人拿到钥匙并到达办公室所需的最少时间(以秒为单位)。
[samples]
## Note
在第一个例子中,位于点 #cf_span[20] 的人应取走位于点 #cf_span[40] 的钥匙,然后前往位于点 #cf_span[50] 的办公室,耗时 #cf_span[30] 秒。位于点 #cf_span[100] 的人可以取走位于点 #cf_span[80] 的钥匙并前往办公室,耗时 #cf_span[50] 秒。因此,经过 #cf_span[50] 秒后,所有人都带着钥匙到达了办公室。
**Definitions**
Let $ n, k, p \in \mathbb{Z}^+ $ denote the number of people, number of keys, and office location, respectively.
Let $ A = (a_1, a_2, \dots, a_n) $ be the sorted sequence of initial positions of the $ n $ people.
Let $ B = (b_1, b_2, \dots, b_k) $ be the sorted sequence of positions of the $ k $ keys.
**Constraints**
1. $ 1 \leq n \leq 1000 $
2. $ n \leq k \leq 2000 $
3. $ 1 \leq p \leq 10^9 $
4. $ 1 \leq a_i \leq 10^9 $ for all $ i \in \{1, \dots, n\} $
5. $ 1 \leq b_j \leq 10^9 $ for all $ j \in \{1, \dots, k\} $
6. All $ a_i $ are distinct; all $ b_j $ are distinct.
**Objective**
Assign exactly $ n $ distinct keys (from the $ k $ available) to the $ n $ people such that the maximum time taken by any person to reach a key and then proceed to the office is minimized.
The time for person $ i $ assigned to key $ j $ is:
$$
t_{i,j} = |a_i - b_j| + |b_j - p|
$$
Let $ \sigma: \{1, \dots, n\} \to \{1, \dots, k\} $ be an injective mapping assigning person $ i $ to key $ \sigma(i) $.
Define the cost of assignment $ \sigma $ as:
$$
C(\sigma) = \max_{1 \leq i \leq n} \left( |a_i - b_{\sigma(i)}| + |b_{\sigma(i)} - p| \right)
$$
Find:
$$
\min_{\sigma \text{ injective}} C(\sigma)
$$
API Response (JSON)
{
"problem": {
"name": "A. Office Keys",
"description": {
"content": "There are _n_ people and _k_ keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key an",
"description_type": "Markdown"
},
"platform": "Codeforces",
"limit": {
"time_limit": 2000,
"memory_limit": 262144
},
"difficulty": "None",
"is_remote": true,
"is_sync": true,
"sync_url": null,
"sign": "CF830A"
},
"statements": [
{
"statement_type": "Markdown",
"content": "There are _n_ people and _k_ keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key an...",
"is_translate": false,
"language": "English"
},
{
"statement_type": "Markdown",
"content": "一条直线上有 #cf_span[n] 个人和 #cf_span[k] 把钥匙。每个人都要到达同样位于这条直线上的办公室。为此,他需要先到达某个有钥匙的点,拿起钥匙,然后前往办公室。一旦某把钥匙被某人取走,其他人就无法再取走它。\n\n你需要确定所有人拿到钥匙并到达办公室所需的最少时间。假设每个人每秒移动一单位距离。如果两个人同时到达同一把钥匙,只有其中一人能取走它。一个人可以经过有钥匙的点而不取走它。...",
"is_translate": true,
"language": "Chinese"
},
{
"statement_type": "Markdown",
"content": "**Definitions** \nLet $ n, k, p \\in \\mathbb{Z}^+ $ denote the number of people, number of keys, and office location, respectively. \nLet $ A = (a_1, a_2, \\dots, a_n) $ be the sorted sequence of initia...",
"is_translate": false,
"language": "Formal"
}
]
}