QOJ.ac

QOJ

時間限制: 2.0 s 記憶體限制: 1024 MB 總分: 100

#17989. Ants on a Ring

统计

在一个圆环上有 $N$ 只蚂蚁。圆环上有 $L$ 个位置,按顺时针方向从 $1$ 到 $L$ 编号,其中位置 $L$ 与位置 $1$ 相邻。蚂蚁们起始于不同的位置,并且想要到达不同的目标位置。为此,在每一秒内,每只蚂蚁可以选择保持原地不动,或者沿圆环移动到相邻的位置(顺时针或逆时针)。

任何两只蚂蚁都不能在同一时间处于圆环上的同一位置,即使是在位置之间移动时也不行。例如,假设在某一秒内,有一只蚂蚁顺时针从位置 $1$ 移动到位置 $2$。在这一秒内,其他蚂蚁不能进行以下任何操作:

  • 在位置 $2$ 保持不动(蚂蚁会在位置 $2$ 相遇)。
  • 逆时针从位置 $3$ 移动到位置 $2$(蚂蚁会在位置 $2$ 相遇)。
  • 逆时针从位置 $2$ 移动到位置 $1$(蚂蚁会在位置 $1$ 和 $2$ 之间相遇)。

确定是否所有蚂蚁都能到达它们的目标位置,如果可以,求出所需的最少秒数。也就是说,求出最小的 $t$,使得在 $t$ 秒后,每只蚂蚁都能处于其目标位置。

输入格式

第一行包含两个整数 $N$($1 \le N \le 1000$)和 $L$($1 \le L \le 10^9$),分别表示蚂蚁的数量和位置的数量。

第二行包含 $N$ 个互不相同的整数 $A_1, A_2, \dots, A_N$(对于 $i = 1, 2, \dots, N$,满足 $1 \le A_i \le L$),其中 $A_i$ 是第 $i$ 只蚂蚁的初始位置。

第三行包含 $N$ 个互不相同的整数 $B_1, B_2, \dots, B_N$(对于 $i = 1, 2, \dots, N$,满足 $1 \le B_i \le L$),其中 $B_i$ 是第 $i$ 只蚂蚁的目标位置。

输出格式

输出单行,包含一个整数,表示所有蚂蚁到达目标位置所需的最短时间;如果无法实现,则输出字符 *(星号)。

样例

输入样例 1

2 2
2 1
2 1

输出样例 1

0

说明 1

两只蚂蚁已经处于它们的目标位置,因此答案为 0。

输入样例 2

2 2
1 2
2 1

输出样例 2

1

说明 2

两只蚂蚁可以同时顺时针或逆时针移动,仅需 1 秒即可到达它们的目标位置。

输入样例 3

1 10
1
7

输出样例 3

4

输入样例 4

3 5
1 3 2
5 2 4

输出样例 4

*

输入样例 5

3 5
1 3 2
4 2 5

输出样例 5

2

说明 5

所有三只蚂蚁都可以逆时针移动,其中第二只蚂蚁保持原地不动一秒。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.