题目描述
Problem Description
This problem is a translation from CEOI 2024 Day2 T3「Sprinklers」
Vaclav has a beautiful garden with a row of $M$ flowers. Along this row, he has placed $N$ sprinklers to water the flowers.
The positions of the sprinklers are given by the numbers $s_1, s_2, \ldots, s_N$. The positions of the flowers are given by the numbers $f_1, f_2, \ldots, f_M$. Both sequences are in non-decreasing order, i.e.:
- $s_1 \leq s_2 \leq \ldots \leq s_N$
- $f_1 \leq f_2 \leq \ldots \leq f_M$
Vaclav is about to attend CEOI. He wants to ensure that all the flowers are properly watered while he is away. To do this, he can individually rotate each sprinkler to the left or right and set their watering intensity—all sprinklers share the same water pipe, so the spray distance is the same.
If the watering intensity is $K$, and the $i$-th sprinkler is rotated to the left, it will water all flowers in the range from $s_i - K$ to $s_i$. Similarly, if the $j$-th sprinkler is rotated to the right, it will water all flowers in the range from $s_j$ to $s_j + K$. A single sprinkler can water multiple flowers, and a flower can be watered by multiple sprinklers.
Your task is to determine if it is possible to water all the flowers. If yes, you should find the minimum sufficient watering intensity and the corresponding sprinkler configuration. If there are multiple valid configurations with the minimum watering intensity, output any one of them.
Input Format
The first line of input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $s_1, s_2, \ldots, s_N$, representing the positions of the sprinklers. The third line contains $M$ space-separated integers $f_1, f_2, \ldots, f_M$, representing the positions of the flowers.
Output Format
If it is not possible to water all the flowers, output the number $-1$.
If it is possible, the output should contain two lines. The first line should output the number $K$, the minimum watering intensity required to water all the flowers. The second line should output a string $c$ of length $N$, where $c_i$ is L if the $i$-th sprinkler should be rotated to the left, otherwise R.
Sample Input and Output
Sample Input #1
3 3
10 10 10
5 11 16
Sample Output #1
6
LLR
Sample Input #2
1 2
1000
1 2000
Sample Output #2
-1
Notes/Hints
Sample Explanation 1
Every flower is watered by at least one sprinkler. A watering intensity less than $6$ is not possible because the flower at position $16$ is $6$ units away from the nearest sprinkler.
Sample Explanation 2
No matter the direction of the sprinkler, at most one flower can be watered at a time.
For all input data, the following constraints apply:
- $1 \leq N, M \leq 10^5$
- $0 \leq s_i \leq 10^9\ (1 \leq i \leq N)$
- $0 \leq f_i \leq 10^9\ (1 \leq i \leq M)$
- $s_i \leq s_j\ (i \leq j)$
- $f_i \leq f_j\ (i \leq j)$
Detailed subtask additional constraints and scores are as follows:
| Subtask | Additional Constraints | Score |
|---|---|---|
| $1$ | $N = 1$ | $3$ |
| $2$ | $N = 3x$, $x$ is an integer, $s_{3i+1}=s_{3i+2}=s_{3i+3}\ (0 \leq i \leq x-1)$ (i.e., sprinklers are always placed in groups of three) | $6$ |
| $3$ | $N \leq 10, M \leq 1,000$ | $17$ |
| $4$ | $K \leq 8$ (i.e., there exists a sprinkler configuration such that a watering intensity of at most $8$ is sufficient to water all flowers) | $27$ |
| $5$ | No additional constraints | $47$ |
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分