diff --git a/CMakeLists.txt b/CMakeLists.txt index e212797..d57756f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/csp-decode/data.in b/csp-decode/data.in new file mode 100644 index 0000000..9330744 --- /dev/null +++ b/csp-decode/data.in @@ -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 diff --git a/palin/data.in b/palin/data.in new file mode 100644 index 0000000..c832e97 --- /dev/null +++ b/palin/data.in @@ -0,0 +1,5 @@ +2 +5 +4 1 2 4 5 3 1 2 3 5 +3 +3 2 1 2 1 3 diff --git a/palin/main.cc b/palin/main.cc new file mode 100644 index 0000000..9f17e53 --- /dev/null +++ b/palin/main.cc @@ -0,0 +1,79 @@ +#include +#include +#include +#include + +using namespace std; + +const int N = 1000005; + +int arr[N], brr[N], crr[N], n; + +bool calc(char op) { + deque 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; + } +}