#include<bits/stdc++.h>
using namespace std;
pair<int, int> dp[1000005][2];
int a[1000005], b[1000005];
pair<int, int> add1(pair<int, int> x)
{
x.first++; x.second++; return x;
}
pair<int, int> unite(pair<int, int> x, pair<int, int> y)
{
//It could be unite, trust me bro
if(x == make_pair(-1, -1)) return y;
return make_pair(min(x.first, y.first), max(x.second, y.second));
}
bool contain(pair<int, int> y, int x)
{
return (y.first <= x && x <= y.second);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n; cin>>n;
n *= 2;
for(int i = 1; i <= n; i++) cin>>a[i];
for(int i = 1; i <= n; i++) cin>>b[i];
for(int i = 1; i <= n; i++) dp[i][0] = dp[i][1] = {-1, -1};
dp[1][1] = {1, 1}, dp[1][0] = {0, 0};
for(int i = 1; i < n; i++){
if(a[i] < a[i+1]) dp[i+1][1] = unite(dp[i+1][1], add1(dp[i][1]));
if(a[i] < b[i+1]) dp[i+1][0] = unite(dp[i+1][0], dp[i][1]);
if(b[i] < a[i+1]) dp[i+1][1] = unite(dp[i+1][1], add1(dp[i][0]));
if(b[i] < b[i+1]) dp[i+1][0] = unite(dp[i+1][0], dp[i][0]);
}
int num = n/2;
if(contain(dp[n][0], num) == 0 && contain(dp[n][1], num) == 0){
cout<<-1;
return 0;
}
string ans;
int last = 2e9;
for(int i = n; i >= 1; i--){
if(contain(dp[i][0], num) == 1 && last > b[i]){
ans += "B"; last = b[i];
}
else{
ans += "A"; num--; last = a[i];
}
}
reverse(ans.begin(), ans.end());
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |