#P80. [CSP-S 2021] Palindromic deque

[CSP-S 2021] Palindromic deque

Person in Charge

Problem Description

Given a positive integer nn and an integer sequence a1,a2,,a2na_1, a_2, \ldots, a_{2 n}, each integer from 11 to nn appears exactly twice in these 2n2n numbers. We perform 2n2n operations with the aim of constructing a sequence b1,b2,,b2nb_1, b_2, \ldots, b_{2 n} of length 2n2n. Initially, the sequence bb is empty, and you may choose one of the following two operations in each step:

  1. Append the first element of sequence aa to the end of bb and remove it from aa.
  2. Append the last element of sequence aa to the end of bb and remove it from aa.

Our goal is to make bb a palindromic sequence, i.e., satisfy bi=b2n+1ib_i = b_{2 n + 1 - i} for all 1in1 \le i \le n. You need to judge whether this goal can be achieved. If feasible, output the lexicographically smallest operation scheme, the details of which are specified in the 【Output Format】 section.

Input Format

Each test point contains multiple test cases.

The first line of input contains an integer TT, representing the number of test cases. For each test case:

  • The first line contains a positive integer nn.
  • The second line contains 2n2n integers a1,a2,,a2na_1, a_2, \ldots, a_{2 n} separated by spaces.

Output Format

Output one line of answer for each test case.

If it is impossible to generate a palindromic sequence, output a single line with -1. Otherwise, output a single line with a string of length 2n2n composed of characters L or R (without spaces), where L denotes Operation 1 (removing the first element) and R denotes Operation 2 (removing the last element).

You are required to output the lexicographically smallest string among all valid schemes.

The rule for lexicographical comparison is as follows: A string s12ns_{1 \sim 2 n} of length 2n2n is lexicographically smaller than a string t12nt_{1 \sim 2 n} of the same length if and only if there exists an index 1k2n1 \le k \le 2 n such that si=tis_i = t_i for all 1i<k1 \le i < k and sk<tks_k < t_k.

Samples

2
5
4 1 2 4 5 3 1 2 3 5
3
3 2 1 2 1 3
LRRLLRRRRL
-1

Sample (1) Explanation

In the first group of data, the generated sequence bb is [4,5,3,1,2,2,1,3,5,4][4, 5, 3, 1, 2, 2, 1, 3, 5, 4], which is clearly a palindromic sequence.

Another possible operation scheme is LRRLLRRRRR, which is lexicographically larger than the answer scheme.

Sample (2)

See palin/palin2.in and palin/palin2.ans in the contestant's directory.

Data Range

Let n\sum n denote the sum of nn values across all TT test cases.

All test cases satisfy 1T1001 \le T \le 100, 1n,n5×1051 \le n, \sum n \le 5 \times 10^5.

Test Case No. TT \le nn \le n\sum n \le Special Property
171 \sim 7 1010 5050 None
8108 \sim 10 100100 2020 10001000
111211 \sim 12 100100
131513 \sim 15 10001000 2500025000
161716 \sim 17 11 5×1055 \times 10^5
182018 \sim 20 100100 Exists
212521 \sim 25 None

Special Property: There exists a way to erase the entire sequence by repeatedly deleting two adjacent and equal numbers from aa (e.g., a=[1,2,2,1]a = [1, 2, 2, 1]).