English · Original
Chinese · Translation
Formal · Original
Luba is surfing the Internet. She currently has _n_ opened tabs in her browser, indexed from 1 to _n_ from left to right. The mouse cursor is currently located at the _pos_\-th tab. Luba needs to use the tabs with indices from _l_ to _r_ (inclusive) for her studies, and she wants to close all the tabs that don't belong to this segment as fast as possible.
Each second Luba can either try moving the cursor to the left or to the right (if the cursor is currently at the tab _i_, then she can move it to the tab _max_(_i_ - 1, _a_) or to the tab _min_(_i_ + 1, _b_)) or try closing all the tabs to the left or to the right of the cursor (if the cursor is currently at the tab _i_, she can close all the tabs with indices from segment \[_a_, _i_ - 1\] or from segment \[_i_ + 1, _b_\]). In the aforementioned expressions _a_ and _b_ denote the minimum and maximum index of an unclosed tab, respectively. For example, if there were 7 tabs initially and tabs 1, 2 and 7 are closed, then _a_ = 3, _b_ = 6.
What is the minimum number of seconds Luba has to spend in order to leave **only the tabs with initial indices from _l_ to _r_ inclusive** opened?
## Input
The only line of input contains four integer numbers _n_, _pos_, _l_, _r_ (1 ≤ _n_ ≤ 100, 1 ≤ _pos_ ≤ _n_, 1 ≤ _l_ ≤ _r_ ≤ _n_) — the number of the tabs, the cursor position and the segment which Luba needs to leave opened.
## Output
Print one integer equal to the minimum number of seconds required to close all the tabs outside the segment \[_l_, _r_\].
[samples]
## Note
In the first test Luba can do the following operations: shift the mouse cursor to the tab 2, close all the tabs to the left of it, shift the mouse cursor to the tab 3, then to the tab 4, and then close all the tabs to the right of it.
In the second test she only needs to close all the tabs to the right of the current position of the cursor.
In the third test Luba doesn't need to do anything.
Luba 正在浏览互联网。她当前在浏览器中打开了 #cf_span[n] 个标签页,从左到右编号为 #cf_span[1] 到 #cf_span[n]。鼠标光标当前位于第 #cf_span[pos] 个标签页。Luba 需要使用编号从 #cf_span[l] 到 #cf_span[r](包含)的标签页进行学习,她希望尽快关闭所有不属于该区间的标签页。
每秒钟,Luba 可以选择向左或向右移动光标(如果光标当前位于第 #cf_span[i] 个标签页,则可以移动到第 #cf_span[max(i - 1, a)] 个或第 #cf_span[min(i + 1, b)] 个标签页),也可以选择关闭光标左侧或右侧的所有标签页(如果光标当前位于第 #cf_span[i] 个标签页,她可以关闭所有编号在区间 #cf_span[[a, i - 1]] 或 #cf_span[[i + 1, b]] 的标签页)。其中,#cf_span[a] 和 #cf_span[b] 分别表示当前未关闭标签页的最小和最大编号。例如,如果最初有 #cf_span[7] 个标签页,而标签页 #cf_span[1]、#cf_span[2] 和 #cf_span[7] 已被关闭,则 #cf_span[a = 3],#cf_span[b = 6]。
Luba 至少需要花费多少秒,才能仅保留初始编号在 #cf_span[l] 到 #cf_span[r](包含)之间的标签页?
输入仅一行,包含四个整数 #cf_span[n], #cf_span[pos], #cf_span[l], #cf_span[r](#cf_span[1 ≤ n ≤ 100], #cf_span[1 ≤ pos ≤ n], #cf_span[1 ≤ l ≤ r ≤ n]),分别表示标签页总数、光标位置和需要保留的区间。
请输出一个整数,表示关闭所有位于区间 #cf_span[[l, r]] 外的标签页所需的最少秒数。
在第一个测试用例中,Luba 可以执行以下操作:将鼠标光标移动到第 #cf_span[2] 个标签页,关闭其左侧所有标签页;再将光标移动到第 #cf_span[3] 个标签页,然后移动到第 #cf_span[4] 个标签页,最后关闭其右侧所有标签页。
在第二个测试用例中,她只需关闭光标当前位置右侧的所有标签页。
在第三个测试用例中,Luba 无需执行任何操作。
## Input
输入仅一行,包含四个整数 #cf_span[n], #cf_span[pos], #cf_span[l], #cf_span[r](#cf_span[1 ≤ n ≤ 100], #cf_span[1 ≤ pos ≤ n], #cf_span[1 ≤ l ≤ r ≤ n]),分别表示标签页总数、光标位置和需要保留的区间。
## Output
请输出一个整数,表示关闭所有位于区间 #cf_span[[l, r]] 外的标签页所需的最少秒数。
[samples]
## Note
在第一个测试用例中,Luba 可以执行以下操作:将鼠标光标移动到第 #cf_span[2] 个标签页,关闭其左侧所有标签页;再将光标移动到第 #cf_span[3] 个标签页,然后移动到第 #cf_span[4] 个标签页,最后关闭其右侧所有标签页。在第二个测试用例中,她只需关闭光标当前位置右侧的所有标签页。在第三个测试用例中,Luba 无需执行任何操作。
Given:
- $ n $: total number of tabs, indexed $ 1 $ to $ n $.
- $ \text{pos} $: initial cursor position, $ 1 \leq \text{pos} \leq n $.
- $ l, r $: target segment of tabs to keep open, $ 1 \leq l \leq r \leq n $.
Define:
- Let $ a $ be the current leftmost open tab (initially $ a = 1 $).
- Let $ b $ be the current rightmost open tab (initially $ b = n $).
Operations (each takes 1 second):
1. Move cursor left: $ \text{pos} \leftarrow \max(\text{pos} - 1, a) $.
2. Move cursor right: $ \text{pos} \leftarrow \min(\text{pos} + 1, b) $.
3. Close all tabs to the left of cursor: set $ a \leftarrow \text{pos} $.
4. Close all tabs to the right of cursor: set $ b \leftarrow \text{pos} $.
Goal:
Minimize the number of operations to reach $ a = l $ and $ b = r $.
**Formal Objective:**
Find the minimum number of operations to transform the state $ (a, b, \text{pos}) = (1, n, \text{pos}) $ into $ (l, r, \text{pos}') $ for some $ \text{pos}' \in [l, r] $, using the allowed operations.
API Response (JSON)
{
"problem": {
"name": "B. Browser",
"description": {
"content": "Luba is surfing the Internet. She currently has _n_ opened tabs in her browser, indexed from 1 to _n_ from left to right. The mouse cursor is currently located at the _pos_\\-th tab. Luba needs to use ",
"description_type": "Markdown"
},
"platform": "Codeforces",
"limit": {
"time_limit": 1000,
"memory_limit": 262144
},
"difficulty": "None",
"is_remote": true,
"is_sync": true,
"sync_url": null,
"sign": "CF915B"
},
"statements": [
{
"statement_type": "Markdown",
"content": "Luba is surfing the Internet. She currently has _n_ opened tabs in her browser, indexed from 1 to _n_ from left to right. The mouse cursor is currently located at the _pos_\\-th tab. Luba needs to use ...",
"is_translate": false,
"language": "English"
},
{
"statement_type": "Markdown",
"content": "Luba 正在浏览互联网。她当前在浏览器中打开了 #cf_span[n] 个标签页,从左到右编号为 #cf_span[1] 到 #cf_span[n]。鼠标光标当前位于第 #cf_span[pos] 个标签页。Luba 需要使用编号从 #cf_span[l] 到 #cf_span[r](包含)的标签页进行学习,她希望尽快关闭所有不属于该区间的标签页。\n\n每秒钟,Luba 可以选择向左或向右移动光标...",
"is_translate": true,
"language": "Chinese"
},
{
"statement_type": "Markdown",
"content": "Given: \n- $ n $: total number of tabs, indexed $ 1 $ to $ n $. \n- $ \\text{pos} $: initial cursor position, $ 1 \\leq \\text{pos} \\leq n $. \n- $ l, r $: target segment of tabs to keep open, $ 1 \\leq l...",
"is_translate": false,
"language": "Formal"
}
]
}