Sprinklers

시간 제한2초메모리 제한1024 MB

요약
직선 위에 정렬된 N개의 살수기와 M개의 꽃 위치가 주어질 때, 각 살수기의 방향과 모든 꽃을 덮는 최소 공통 분사 거리 K를 구한다.
난이도

보통10점 중 7점

유형
그리디, 이분 탐색, 투 포인터, 정렬
정답자
아직 제출이 없습니다

문제

Václav has a beautiful flower garden consisting of MM flowers planted on a single line. On this line, Václav has also placed NN sprinklers to water his flowers.

The positions of the sprinklers are given by the numbers s_1,…,s_Ns\_{1},\ldots,s\_{N}. The positions of the flowers are given by the numbers f_1,…,f_Mf\_{1},\ldots,f\_{M}. Both are provided in non-decreasing order, that is:

  • s_1≤s_2≤…≤s_Ns\_{1}\leq s\_{2}\leq\ldots\leq s\_{N}
  • f_1≤f_2≤…≤f_Mf\_{1}\leq f\_{2}\leq\ldots\leq f\_{M}

Václav is leaving for CEOI soon. He would like to make sure that all of his flowers are properly watered while he is away. To do this, he turns each sprinkler individually to the left or to the right, and sets their spraying power — all sprinklers share the same water hose, and therefore spray the same distance.

If the spraying power is KK and the ii-th sprinkler is turned to the left, it will water all flowers with positions between s_i−Ks\_{i} - K and s_is\_{i} (inclusive). Similarly, if the jj-th sprinkler is turned to the right, it will water all flowers with positions between s_js\_{j} and s_j+Ks\_{j} + K (inclusive). A single sprinkler can water multiple flowers and a single flower can be watered by multiple sprinklers.

Your task is to decide whether it's possible to water all the flowers. If so, you should find the minimum sufficient spraying power, along with a corresponding configuration of sprinklers. If there exist multiple valid configurations with minimal spraying power, output any of them.

입력

The first line of input contains two integers: NN and MM, separated by a space. The second line contains NN space-separated integers s_1,…,s_Ns\_{1},\ldots,s\_{N} — the positions of the sprinklers. The third line contains MM space-separated integers f_1,…,f_Mf\_{1},\ldots,f\_{M} — the positions of the flowers.

출력

If it is not possible to water all the flowers, print the number −1-1.

If it is possible, the output should consist of two lines. On the first line, output the number KK – the minimum spraying power required to water all the flowers. On the second line, print a string cc of length NN, such that c_ic\_i is L if the ii-th sprinkler should be turned to the left and R otherwise.

제한

  • 1≤N,M≤1051 \leq N,M \leq 10^5
  • 0≤s_i≤1090 \leq s\_{i} \leq 10^9 (for each ii such that 1≤i≤N1 \leq i \leq N)
  • 0≤f_i≤1090 \leq f\_{i} \leq 10^9 (for each ii such that 1≤i≤M1 \leq i \leq M)
  • s_i≤s_js\_{i} \leq s\_{j} for all i≤ji \leq j
  • f_i≤f_jf\_{i} \leq f\_{j} for all i≤ji \leq j

예제2

  1. 예제 1

    입력
    3 3
    10 10 10
    5 11 16
    
    예상 출력
    6
    LLR
    
  2. 예제 2

    입력
    1 2
    1000
    1 2000
    
    예상 출력
    -1