QOJ.ac

QOJ

Time Limit: 2.0 s Memory Limit: 128 MB Total points: 100

#5332. 弹珠

Statistics

给定一条长度为 $L$ 的轨道。轨道上有 $N$ 个弹珠,位置分别为 $A_1, A_2, \dots, A_N$,以及 $N$ 个开关,位置分别为 $B_1, B_2, \dots, B_N$,它们的大小均可忽略不计。初始时,你为每个弹珠指定向左或向右的运动方向,它们将以恒定的速度 $1$ 开始运动。当两个弹珠相撞时,它们会发生弹性碰撞,即以相同的速度反向运动。如果弹珠撞到轨道的起点或终点,它会以相同的速度反向弹回。请注意,弹珠从位置 $i$ 移动到 $i+1$ 或 $i-1$ 需要恰好 $1$ 秒,从 $1$ 变向回到 $1$ 或从 $L$ 变向回到 $L$ 也需要恰好 $1$ 秒。弹珠不会停在开关上。

目标是使每个开关上都有一个弹珠,即所有弹珠同时位于对应的开关位置上。

你需要求出实现这一目标所需的最短时间。

输入格式

第一行输入 $L$ ($L \le 10^9$) 和 $N$ ($N \le 3000$)。

第二行输入 $A_1, A_2, \dots, A_N$,表示弹珠的初始位置。保证所有 $A_i$ 互不相同,且 $1 \le A_i \le L$。

第三行输入 $B_1, B_2, \dots, B_N$,表示开关的位置。保证所有 $B_i$ 互不相同,且 $1 \le B_i \le L$。

输出格式

输出一个整数:使每个开关上都有一个弹珠所需的最短时间。如果不存在解决方案,输出 $-1$。

样例

输入 1

4 2
2 4
1 4

输出 1

1

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.