이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int inf = 10 + 5;
struct segm {
int l = inf, r = 0;
segm operator ++() {
r++;
l++;
return *this;
}
segm operator |(const segm& x) const { return segm{min(l, x.l), max(r, x.r)}; }
segm operator |=(const segm& x) { return (*this = *this | x); }
bool operator ==(const int& x) const { return l <= x && x <= r; }
};
int main() {
int n;
cin >> n;
vector<int> A(2 * n + 1), B(2 * n + 1);
vector<segm> atrA(2 * n + 1), atrB(2 * n + 1);
for(int i = 1; i <= 2 * n; i++) cin >> A[i];
for(int i = 1; i <= 2 * n; i++) cin >> B[i];
A[0] = B[0] = -1;
atrA[0] = segm{0, 0};
atrB[0] = segm{0, 0};
for(int i = 1; i <= 2 * n; i++) {
if(A[i] >= A[i - 1]) atrA[i] |= atrA[i - 1];
if(A[i] >= B[i - 1]) atrA[i] |= atrB[i - 1];
if(B[i] >= A[i - 1]) atrB[i] |= atrA[i - 1];
if(B[i] >= B[i - 1]) atrB[i] |= atrB[i - 1];
++atrA[i];
int mn = min(A[i - 1], B[i - 1]);
if(A[i] < mn)
A[i] = inf;
if(B[i] < mn)
B[i] = inf;
//cerr << A[i] << '\t' << B[i] << '\n' << atrA[i].l << ' ' << atrA[i].r << ", " << atrB[i].l << ' ' << atrB[i].r << '\n';
}
auto decompose = [&](auto&& self, int poz, int target, int type) -> void {
if(poz == 0)
return;
int val = (type == 0? A[poz] : B[poz]), ch = (type == 0? 'A' : 'B');
if(A[poz - 1] <= val && atrA[poz - 1] == target)
self(self, poz - 1, target - 1, 0);
else
self(self, poz - 1, target, 1);
cout << (char)ch;
};
if(atrA[2 * n] == n)
decompose(decompose, 2 * n, n - 1, 0);
else if(atrB[2 * n] == n)
decompose(decompose, 2 * n, n, 1);
else
cout << "-1";
cout << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |