听说这里回的比较快 P7915
#include<bits/stdc++.h>
using namespace std;
#define maxn 1000006
int read(){
int x=0;
int f=1;
char c=getchar();
while(c>'9'||c<'0'){
if(c=='-')f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
x=(x<<1)+(x<<3)+(c^'0');
c=getchar();
}
return x*f;
}
int t,n,pl,pr,pll,prr,a[maxn];
int vis[maxn];
vector<int>front_half;
stack<int>back_half;
int tag[maxn>>1];
//某位置对应的某数的对应另一个数的位置
int temp_tag[maxn>>1];
//第一个某数的位置
int left_first(){
pl=1;pr=2*n+1;
pll=prr=tag[pl];
//cout<<pll<<endl;
front_half.push_back(1);
back_half.push(a[pll]);
vis[a[pl]]=1;
for(int k=1;k<=n;k++){
if(k==n){
for(int i=0;i<n;i++){
if(front_half[i]==1)cout<<'L';
else cout<<'R';
}
// cout<<' ';
while(!back_half.empty()){
int temp=back_half.top();
back_half.pop();
if(a[pll]==temp){
cout<<'L';
pll++;
}else{
cout<<'R';
prr--;
}
}
cout<<'\n';
}else{
// cout<<pl<<' '<<pll<<' '<<prr<<' '<<pr<<endl;
if(vis[a[pl+1]]==0&&(tag[pl+1]==pll-1||tag[pl+1]==prr+1)){//左边可选
//判断条件可调?
pl++;
if(tag[pl]==pll-1)pll--;
else prr++;
vis[a[pl]]=1;
front_half.push_back(1);
back_half.push(a[pl]);
// cout<<a[pl]<<endl;
}else if(vis[a[pr-1]]==0&&(tag[pr-1]==pll-1||tag[pr-1]==prr+1)){//右边可选
//同可调?
pr--;
if(tag[pr]==prr+1)prr++;
else pll--;
vis[a[pr]]=1;
front_half.push_back(2);
back_half.push(a[pr]);
// cout<<a[pr]<<endl;
}else{//都不可选返回负一
return 0;
}
}
}
return 1;
}
int right_first(){
for(int i=1;i<=2*n;i++)vis[i]=0;
pl=0;pr=2*n;
pll=prr=tag[pr];
front_half.clear();
while(!back_half.empty()) back_half.pop();
vis[a[pr]]=1;
back_half.push(a[pll]);
front_half.push_back(2);
for(int k=1;k<=n;k++){
if(k==n){
for(int i=0;i<n;i++){
if(front_half[i]==1)cout<<'L';
else cout<<'R';
}
// cout<<' ';
while(!back_half.empty()){
int temp=back_half.top();
back_half.pop();
if(a[pll]==temp){
cout<<'L';
pll++;
}else{
cout<<'R';
prr--;
}
}
cout<<'\n';
}else{
// cout<<pl<<' '<<pll<<' '<<prr<<' '<<pr<<endl;
if(vis[a[pl+1]]==0&&(tag[pl+1]==pll-1||tag[pl+1]==prr+1)){//左边可选
//判断条件可调?
pl++;
if(tag[pl]==pll-1)pll--;
else prr++;
vis[a[pl]]=1;
front_half.push_back(1);
back_half.push(a[pl]);
// cout<<a[pl]<<endl;
}else if(vis[pr-1]==0&&(tag[pr-1]==pll-1||tag[pr-1]==prr+1)){//右边可选
//同可调?
pr--;
if(tag[pr]==prr+1)prr++;
else pll--;
vis[a[pr]]=1;
front_half.push_back(2);
back_half.push(a[pr]);
// cout<<a[pr]<<endl;
}else{//都不可选返回负
return 0;
}
}
}
return 1;
}
int main(){
t=read();
while(t--){
n=read();
while(back_half.empty() == 0) back_half.pop(); front_half.clear();
for(int i=1;i<=2*n;i++){
vis[i]=0;
temp_tag[i]=0;
tag[i]=0;
}
for(int i=1;i<=2*n;i++){
a[i]=read();
if(temp_tag[a[i]]){
tag[temp_tag[a[i]]]=i;
tag[i]=temp_tag[a[i]];
}
else{
temp_tag[a[i]]=i;
}
}
if(left_first()==0){
if(right_first()==0)cout<<-1<<'\n';
}
}
return 0;
}