-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuogu_P_7915.cpp
142 lines (142 loc) · 4.57 KB
/
Luogu_P_7915.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <bits/extc++.h>
using namespace std;
namespace pbds = __gnu_pbds;
using ui = unsigned int;
using uli = unsigned long long int;
using li = long long int;
int main(void) {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
size_t T;
cin >> T;
while (T--) {
size_t n;
cin >> n;
vector<ui> a(n << 1);
for (ui& i : a) cin >> i;
vector<bool> ans1, ans2;
bool flag = true;
{
size_t p = 1;
for (; p < a.size(); ++p)
if (a[p] == a.front()) break;
ans1.push_back(false), ans2.push_back(false);
deque<size_t> ql, qr;
for (size_t i = 1; i < p; ++i) ql.push_back(i);
for (size_t i = p + 1; i < a.size(); ++i) qr.push_front(i);
while (!ql.empty() && !qr.empty()) {
if (ql.size() > 1 && a[ql.front()] == a[ql.back()])
ql.pop_front(), ql.pop_back(), ans1.push_back(false),
ans2.push_back(false);
else if (a[ql.front()] == a[qr.back()])
ql.pop_front(), qr.pop_back(), ans1.push_back(false),
ans2.push_back(true);
else if (a[qr.front()] == a[ql.back()])
qr.pop_front(), ql.pop_back(), ans1.push_back(true),
ans2.push_back(false);
else if (qr.size() > 1 && a[qr.front()] == a[qr.back()])
qr.pop_front(), qr.pop_back(), ans1.push_back(true),
ans2.push_back(true);
else {
flag = false;
break;
}
}
if (ql.size() > 1) {
if (!qr.empty() || ql.size() & 1) flag = false;
while (!ql.empty()) {
if (a[ql.front()] != a[ql.back()]) {
flag = false;
break;
}
ans1.push_back(false), ans2.push_back(false), ql.pop_front(),
ql.pop_back();
}
}
if (qr.size() > 1) {
if (!ql.empty() || qr.size() & 1) flag = false;
while (!qr.empty()) {
if (a[qr.front()] != a[qr.back()]) {
flag = false;
break;
}
ans1.push_back(true), ans2.push_back(true), qr.pop_front(),
qr.pop_back();
}
}
if (ql.size() == 1 && qr.size() == 1)
ans1.push_back(false), ans2.push_back(true);
}
if (flag) {
for_each(ans1.cbegin(), ans1.cend(),
[](bool b) { cout.put(!b ? 'L' : 'R'); });
for_each(ans2.crbegin(), ans2.crend(),
[](bool b) { cout.put(!b ? 'L' : 'R'); });
cout.put('\n');
continue;
}
flag = true;
ans1.clear(), ans2.clear();
reverse(a.begin(), a.end());
{
size_t p = 1;
for (; p < a.size(); ++p)
if (a[p] == a.front()) break;
ans1.push_back(false), ans2.push_back(true);
deque<size_t> ql, qr;
for (size_t i = 1; i < p; ++i) ql.push_back(i);
for (size_t i = p + 1; i < a.size(); ++i) qr.push_front(i);
while (!ql.empty() && !qr.empty()) {
if (qr.size() > 1 && a[qr.front()] == a[qr.back()])
qr.pop_front(), qr.pop_back(), ans1.push_back(true),
ans2.push_back(true);
else if (a[qr.front()] == a[ql.back()])
qr.pop_front(), ql.pop_back(), ans1.push_back(true),
ans2.push_back(false);
else if (a[ql.front()] == a[qr.back()])
ql.pop_front(), qr.pop_back(), ans1.push_back(false),
ans2.push_back(true);
else if (ql.size() > 1 && a[ql.front()] == a[ql.back()])
ql.pop_front(), ql.pop_back(), ans1.push_back(false),
ans2.push_back(false);
else {
flag = false;
break;
}
}
if (ql.size() > 1) {
if (!qr.empty() || ql.size() & 1) flag = false;
while (!ql.empty()) {
if (a[ql.front()] != a[ql.back()]) {
flag = false;
break;
}
ans1.push_back(false), ans2.push_back(false), ql.pop_front(),
ql.pop_back();
}
}
if (qr.size() > 1) {
if (!ql.empty() || qr.size() & 1) flag = false;
while (!qr.empty()) {
if (a[qr.front()] != a[qr.back()]) {
flag = false;
break;
}
ans1.push_back(true), ans2.push_back(true), qr.pop_front(),
qr.pop_back();
}
}
if (ql.size() == 1 && qr.size() == 1)
ans1.push_back(true), ans2.push_back(false);
}
if (flag) {
for_each(ans1.cbegin(), ans1.cend(),
[](bool b) { cout.put(b ? 'L' : 'R'); });
for_each(ans2.crbegin(), ans2.crend(),
[](bool b) { cout.put(b ? 'L' : 'R'); });
cout.put('\n');
continue;
}
cout << "-1\n";
}
return 0;
}