luogu#P7915. [CSP-S 2021] 回文

    ID: 7269 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>字符串贪心2021O2优化CSP-S 提高级

[CSP-S 2021] 回文

Problem Description

Given a positive integer nn and an integer sequence a1,a2,,a2na_1, a_2, \ldots, a_{2 n}, among these 2n2 n numbers, each of 1,2,,n1, 2, \ldots, n appears exactly 22 times. Now perform 2n2 n operations, with the goal of creating a sequence b1,b2,,b2nb_1, b_2, \ldots, b_{2 n} of the same length 2n2 n. Initially, bb is an empty sequence. Each time, you may perform one of the following two operations:

  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, that is, it satisfies for all 1in1 \le i \le n, bi=b2n+1ib_i = b_{2 n + 1 - i}. Please determine whether this goal can be achieved. If it can, output the lexicographically smallest operation plan, as explained in Output Format.

Input Format

Each test point contains multiple groups of testdata.

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

The first line contains a positive integer nn.
The second line contains 2n2 n space-separated integers a1,a2,,a2na_1, a_2, \ldots, a_{2 n}.

Output Format

For each test case, output one line as the answer.

If it is impossible to generate a palindromic sequence, output a line -1. Otherwise, output a string of length 2n2 n consisting of characters L or R (with no spaces), where L means removing the first element (operation 1), and R means operation 2.

You need to output the lexicographically smallest string among all valid plans.

The lexicographical order is defined as follows: for two strings s12ns_{1 \sim 2 n} and t12nt_{1 \sim 2 n} of length 2n2 n, ss is lexicographically smaller than tt if and only if there exists an index 1k2n1 \le k \le 2 n such that for every 1i<k1 \le i < k, si=tis_i = t_i, and sk<tks_k < t_k.

2
5
4 1 2 4 5 3 1 2 3 5
3
3 2 1 2 1 3

LRRLLRRRRL
-1

见附件中的 palin/palin2.in
见附件中的 palin/palin2.ans

Hint

Sample Explanation #1

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], and you can see that this is a palindromic sequence.

Another possible operation plan is LRRLLRRRRR, but its lexicographical order is larger than the answer.

Constraints

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

For all test points, it is guaranteed that 1T1001 \le T \le 100, 1n,n5×1051 \le n, \sum n \le 5 \times {10}^5.

Test Point ID 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 Yes
212521 \sim 25 None

Special Property: If each time we delete two adjacent equal numbers in aa, there exists a way to delete the entire sequence (for example, a=[1,2,2,1]a = [1, 2, 2, 1]).

Hack testdata provided by
@潜在了H2O下面

Translated by ChatGPT 5