Codeforces Round 1098 (Div. 2)
B
Remilia Plays Soku
Codeforces
1100
原题
← AC1 →
翻译中…

Remilia is trying to escape, while Reimu wants to land the final hit.

翻译中…

The playing field consists of $n$ positions arranged in a circle. For each $1 \le i \lt n$, positions $i$ and $i+1$ are adjacent, and positions $1$ and $n$ are also adjacent.

翻译中…

Initially, at time $0$, Reimu is at position $x_1$ and Remilia is at position $x_2$, where $x_1 \ne x_2$.

翻译中…

Each second, the following happens in order:

翻译中…
  1. Remilia either moves to an adjacent position or stays in place. Over the entire game, she may move to an adjacent position at most $k$ times.
  2. After observing Remilia's action, Reimu either moves to an adjacent position or stays in place.
  3. If they are at the same position after both actions, Reimu catches Remilia and the game ends.
翻译中…

Assuming both players play optimally, Reimu always tries to minimize the catching time, while Remilia tries to maximize it.

翻译中…

Find the number of seconds until Reimu catches Remilia.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The only line of each test case contains four integers $n$, $x_1$, $x_2$ and $k$ ($2\le n\le10^8$, $1\le x_1,x_2\le n$, $x_1\ne x_2$, $0\le k\le 10^8$).

翻译中…

Output

翻译中…

For each test case, output the number of seconds until Reimu catches Remilia, assuming both players play optimally.

翻译中…

Note

翻译中…

In the first test case, one possible sequence of actions is:

翻译中…
  • In the first second, Remilia stays in place, and then Reimu moves to $2$ and catches Remilia.
翻译中…

In the second test case, one possible sequence of actions is:

翻译中…
  • In the first second, Remilia moves to $1$, and then Reimu moves to $2$.
  • In the second second, Remilia cannot move and has to stay in place, and then Reimu moves to $1$ and catches Remilia.
翻译中…

样例

Input #1
4
2 1 2 0
4 3 2 1
4 2 3 1
16 8 4 2
Output #1
1
2
2
6
InputCopy OutputCopy
题目信息
题号
2228B
时间限制
1 s
内存限制
256 MB
标签
博弈论
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。