#P16547. [ICPC 2026 LAC] Ants on a Ring

[ICPC 2026 LAC] Ants on a Ring

题目描述

There are NN ants on a circle. The circle has LL spots numbered from 11 to LL clockwise, with spot LL adjacent to spot 11. Ants start at different spots, and want to reach different spots. To do so, in one second each ant may stay still or move around the circle to an adjacent spot (clockwise or counterclockwise).

No two ants can be at the same position on the circle at the same time, even between spots. For instance, suppose that during a second an ant moves clockwise from spot 11 to spot 22. During that second, other ants cannot do any of the following:

  • Stay still at spot 22 (ants would meet at spot 22).
  • Move counterclockwise from spot 33 to spot 22 (ants would meet at spot 22).
  • Move counterclockwise from spot 22 to spot 11 (ants would meet between spots 11 and 22).

Determine whether it is possible for all ants to reach their targets, and if so, the minimum number of seconds required. That is, find the minimum tt such that after tt seconds, every ant can be at its target spot.

输入格式

The first line contains two integers NN (1N10001 \le N \le 1000) and LL (1L1091 \le L \le 10^9), indicating respectively the number of ants and the number of spots.

The second line contains NN different integers A1,A2,,ANA_1, A_2, \ldots, A_N (1AiL1 \le A_i \le L for i=1,2,,Ni = 1, 2, \ldots, N), where AiA_i is the initial spot of the ii-th ant.

The third line contains NN different integers B1,B2,,BNB_1, B_2, \ldots, B_N (1BiL1 \le B_i \le L for i=1,2,,Ni = 1, 2, \ldots, N), such that BiB_i is the target spot of the ii-th ant.

输出格式

Output a single line with an integer indicating the minimum time required for all ants to reach their targets, or the character “*” (asterisk) if it is impossible.

2 2
2 1
2 1
0
2 2
1 2
2 1
1
1 10
1
7
4
3 5
1 3 2
5 2 4
*
3 5
1 3 2
4 2 5
2

提示

Explanation of Sample 1:

The two ants start at their target spots, so the answer is 00.

Explanation of Sample 2:

The two ants can move simultaneously either clockwise or counterclockwise, reaching their targets after just 11 second.

Explanation of Sample 5:

All three ants can move counterclockwise, with the second ant staying still for a second.