#P7915. Palindromic Deque Operations

    ID: 21100 Type: Default 1000ms 256MiB

Palindromic Deque Operations

Palindromic Deque Operations

Given a positive integer \(n\) and a sequence of \(2n\) integers \(a_1, a_2, \ldots, a_{2n}\) in which each number from \(1\) to \(n\) appears exactly twice, you are to perform \(2n\) operations to construct a sequence \(b_1, b_2, \ldots, b_{2n}\). Initially, \(b\) is empty. In each operation, you may choose one of the following actions:

  1. Append the first element of \(a\) to the end of \(b\) and remove it from \(a\).
  2. Append the last element of \(a\) to the end of \(b\) and remove it from \(a\).

The goal is to have \(b\) become a palindrome, that is, for every \(1 \le i \le 2n\), \(b_i = b_{2n+1-i}\) (in particular, when \(b\) is of even length the condition must hold for all positions). If it is possible to achieve such a \(b\), output the lexicographically smallest sequence of operations (represented as a string of characters where 'L' means taking from the front and 'R' means taking from the back) which accomplishes this; otherwise, output \(-1\).

Note: Lexicographical order is defined by the usual order of characters with \('L' .

inputFormat

The first line contains a positive integer \(n\). The second line contains \(2n\) integers \(a_1, a_2, \ldots, a_{2n}\) separated by spaces.

outputFormat

If a palindrome can be formed by performing the operations, output the lexicographically smallest sequence of operations (a string consisting of the letters 'L' and 'R'); if not, output \(-1\).

sample

3
1 2 3 3 2 1
LLLLLL

</p>