P7915 100pts
This commit is contained in:
LittleSheep 2024-10-11 22:00:06 +08:00
parent f284d184fd
commit 2ec168d4be
4 changed files with 97 additions and 1 deletions

View File

@ -25,4 +25,5 @@ add_executable(Playground
network-connection/main.cpp
eating-snake/main.cc
insert-sort/main.cc
csp-decode/main.cc)
csp-decode/main.cc
palin/main.cc)

11
csp-decode/data.in Normal file
View File

@ -0,0 +1,11 @@
10
770 77 5
633 1 211
545 1 499
683 3 227
858 3 257
723 37 13
572 26 11
867 17 17
829 3 263
528 4 109

5
palin/data.in Normal file
View File

@ -0,0 +1,5 @@
2
5
4 1 2 4 5 3 1 2 3 5
3
3 2 1 2 1 3

79
palin/main.cc Normal file
View File

@ -0,0 +1,79 @@
#include <iostream>
#include <deque>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1000005;
int arr[N], brr[N], crr[N], n;
bool calc(char op) {
deque<int> a, b;
string out, append;
out.push_back(op);
append.push_back('L');
if (op == 'L') {
for (int i = 2; i < crr[1]; i++) a.push_back(i);
for (int i = n; i > crr[1]; i--) b.push_back(i);
} else {
for (int i = 1; i < crr[n]; i++) a.push_back(i);
for (int i = n - 1; i > crr[n]; i--) b.push_back(i);
}
while (!a.empty() || !b.empty()) {
int x1 = !a.empty() ? a.front() : 0;
int x2 = !b.empty() ? b.front() : 0;
int y1 = !a.empty() ? a.back() : 0;
int y2 = !b.empty() ? b.back() : 0;
if (crr[x1] == y1) {
out.push_back('L');
append.push_back('L');
a.pop_front();
a.pop_back();
} else if (crr[x1] == y2) {
out.push_back('L');
append.push_back('R');
a.pop_front();
b.pop_back();
} else if (crr[x2] == y1) {
out.push_back('R');
append.push_back('L');
b.pop_front();
a.pop_back();
} else if (crr[x2] == y2) {
out.push_back('R');
append.push_back('R');
b.pop_front();
b.pop_back();
} else {
return false;
}
}
reverse(append.begin(), append.end());
out += append;
cout << out << endl;
return true;
}
int main() {
int T;
cin >> T;
while (T--) {
cin >> n;
memset(brr, 0, sizeof brr);
n *= 2;
crr[0] = -1;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
if (brr[arr[i]]) {
crr[brr[arr[i]]] = i;
crr[i] = brr[arr[i]];
} else {
brr[arr[i]] = i;
}
}
if (!calc('L') && !calc('R')) cout << -1 << endl;
}
}