No.3754 Mischievous Resident (Hard)
Problems A and E share the same setting, but ask for different things. In Problem E, you must also find the minimum number of operations required to reach the target configuration.
Problem Statement
There is a building with $N$ floors and $M$ elevators. The elevators are numbered $1,2,\ldots,M$ and are distinguishable from one another.
Initially, elevator $i$ is stopped at floor $S_i$. Multiple elevators may be stopped at the same floor.
You can move using the stairs and press the "call button" on each floor any number of times. When you press the button on floor $F$ $(1 \leq F \leq N)$, exactly $1$ elevator moves according to the following rules.
- The elevator whose distance $|x-F|$ from its current position $x$ to floor $F$ is the smallest moves to floor $F$.
- If there are multiple elevators with the smallest distance, you may freely choose $1$ of them to move.
You are given an integer sequence $G_1,G_2,\ldots,G_M$ representing the target configuration of the elevators.
You are given $Q$ test cases. For each test case, consider whether it is possible, by pressing call buttons any number of times, to have elevator $i$ stopped at floor $G_i$ for all $i$.
If the target configuration is reachable,
find the minimum number of call button presses required to reach the configuration.
If the target configuration cannot be reached, output -1.
Constraints
- $1 \leq Q \leq 2 \times 10^5$
- $1 \leq N \leq 10^9$
- $1 \leq M \leq 2 \times 10^5$
- $1 \leq S_i \leq N$
- $1 \leq G_i \leq N$
- The sum of $M$ over all test cases is at most $2 \times 10^5$
- All input values are integers
Input
The input is given from standard input in the following format. Here, the $q\ (1 \leq q \leq Q)$-th test case is denoted by $\mathrm{case}_q$.
$Q$
$\mathrm{case}_1$
$\mathrm{case}_2$
$\vdots$
$\mathrm{case}_Q$
Each test case is given in the following format.
$N$ $M$ $S_1$ $S_2$ $\ldots$ $S_M$ $G_1$ $G_2$ $\ldots$ $G_M$
Here, $S_i$ represents the initial position of elevator $i$, and $G_i$ represents the target position of elevator $i$.
Output
Output $Q$ lines.
On line $q$, for the $q$-th test case, output -1 if the target configuration cannot be reached.
If the target configuration can be reached, output the minimum number of call button presses required.
Sample
Sample 1
Input
5 10 3 8 5 2 9 6 3 10 2 2 8 8 2 10 3 5 5 8 6 4 9 20 2 1 14 5 7 10 1 5 5
Output
3 -1 3 3 0
In the $1$st test case, for example, pressing the buttons on floors $9$, $6$, and $3$ in this order achieves the target configuration. The number of operations in this case is $3$, which is the minimum.
In the $2$nd test case, the target configuration cannot be achieved no matter how the buttons are pressed, so output $-1$. Note that the elevators are distinguishable from one another.
In the $5$th test case, the initial configuration already matches the target configuration, so the minimum number of operations required is $0$.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
siganai