#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
struct segment{
int l, r;
};
void solve(){
int n;
cin >> n;
vector<int> a(n*2+1), b(n*2+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] = 0;
vector<vector<segment>> f(n*2+1, vector<segment>(2, {-1, -1}));
auto merge = [&](segment &a, segment b) -> void{
if(b.l == -1) return;
if(a.l == -1){
a = b;
return;
}
a.l = min(a.l, b.l);
a.r = max(a.r, b.r);
};
f[0][0] = {0, 0};
for(int i = 1; i <= n*2; i++){
//AA
if(a[i] >= a[i-1]){
if(f[i-1][0].l != -1){
merge(f[i][0], {f[i-1][0].l+1, f[i-1][0].r+1});
}
}
//BA
if(a[i] >= b[i-1]){
if(f[i-1][1].l != -1){
merge(f[i][0], {f[i-1][1].l+1, f[i-1][1].r+1});
}
}
//AB
if(b[i] >= a[i-1]){
if(f[i-1][0].l != -1){
merge(f[i][1], f[i-1][0]);
}
}
//BB
if(b[i] >= b[i-1]){
if(f[i-1][0].l != -1){
merge(f[i][1], f[i-1][1]);
}
}
}
if((f[n*2][0].l > n || f[n*2][0].r < n) && (f[n*2][1].l > n || f[n*2][1].r < n)){
cout << -1;
return;
}
vector<char> ans;
int p = n*2, cur = n;
while(p > 0){
if(f[p][0].l <= cur && cur <= f[p][0].r){
ans.push_back('A');
cur--;
}else{
ans.push_back('B');
}
p--;
}
reverse(ans.begin(), ans.end());
for(auto it : ans) cout << it;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |