#P10806. [CEOI 2024] 洒水器

[CEOI 2024] 洒水器

Problem Description

This problem is translated from CEOI 2024 Day 2 T3「Sprinklers」.

Vaclav has a beautiful garden, where he planted a row of MM flowers. Along this row, Vaclav also placed NN sprinklers to water the flowers.

The sprinkler positions are given by numbers s1,s2,,sNs_1, s_2, \ldots , s_N. The flower positions are given by numbers f1,f2,,fMf_1, f_2, \ldots , f_M. Both sequences are in non-decreasing order, that is:

  • s1s2sNs_1 \leq s_2 \leq \ldots \leq s_N.
  • f1f2fMf_1 \leq f_2 \leq \ldots \leq f_M.

Vaclav will soon leave to attend CEOI. He wants to make sure that all flowers will be watered properly while he is away. To do this, he can rotate each sprinkler individually to the left or to the right, and set their spray power. Since all sprinklers share the same water pipe, the spray range is the same for all of them.

If the spray power is KK and the ii-th sprinkler is rotated to the left, it will water all flowers at positions between siKs_i - K and sis_i. Similarly, if the jj-th sprinkler is rotated to the right, it will water all flowers at positions between sjs_j and sj+Ks_j + K. One sprinkler can water multiple flowers, and one flower can be watered by multiple sprinklers.

Your task is to decide whether it is possible to water all the flowers. If it is, you should find the smallest spray power that is sufficient, and a corresponding sprinkler configuration. If there are multiple valid configurations with the minimum spray power, output any one of them.

Input Format

The first line contains two integers NN and MM, separated by spaces. The second line contains NN integers s1,s2,,sNs_1, s_2, \ldots , s_N, separated by spaces, representing the sprinkler positions. The third line contains MM integers f1,f2,,fMf_1, f_2, \ldots , f_M, separated by spaces, representing the flower positions.

Output Format

If it is impossible to water all flowers, output the number 1-1.

If it is possible, the output should contain two lines. The first line outputs the number KK, the minimum spray power needed to water all flowers. In the second line, output a string cc of length NN, where cic_i is L if the ii-th sprinkler should be rotated to the left, otherwise it is R.

3 3
10 10 10
5 11 16
6
LLR
1 2
1000
1 2000
-1

Hint

Sample Explanation 1

Each flower is watered by at least one sprinkler. A spray power smaller than 66 is impossible, because the flower at position 1616 is at distance 66 from the nearest sprinkler.

Sample Explanation 2

No matter how the sprinklers are oriented, at most one flower can be watered at a time.

For all input data, it holds that:

  • 1N,M1051 \leq N, M \leq 10^5.
  • 0si109 (1iN)0 \leq s_{i} \leq 10^9\ (1 \leq i \leq N).
  • 0fi109 (1iM)0 \leq f_{i} \leq 10^9\ (1 \leq i \leq M).
  • sisj (ij)s_{i} \leq s_{j}\ (i \leq j).
  • fifj (ij)f_{i} \leq f_{j}\ (i \leq j).

The detailed additional constraints and scores for subtasks are given in the table below.

Subtask Additional Constraints Score
11 N=1N = 1 33
22 N=3xN = 3x, where xx is an integer, and $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) 66
33 N10,M1000N \leq 10, M \leq 1\,000 1717
44 K8K \leq 8 (i.e., for all testdata, there exists a sprinkler configuration such that a spray power of at most 88 is sufficient to water all flowers) 2727
55 No additional constraints 4747

Translated by ChatGPT 5