#include <bits//stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_multiset;
#define ll long long
#define ld long double
#define iloop(m, h) for (auto i = m; i != h; i += (m < h ? 1 : -1))
#define jloop(m, h) for (auto j = m; j != h; j += (m < h ? 1 : -1))
#define kloop(m, h) for (auto k = m; k != h; k += (m < h ? 1 : -1))
#define lloop(m, h) for (auto l = m; l != h; l += (m < h ? 1 : -1))
#define iloop_(m, h, g) for (auto i = m; i < h; i += g)
#define jloop_(m, h, g) for (auto j = m; j < h; j += g)
#define kloop_(m, h, g) for (auto k = m; k < h; k += g)
#define lloop_(m, h, g) for (auto l = m; l < h; l += g)
#define getchar_unlocked _getchar_nolock // comment before submission
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define pllll pair<pll, pll>
#define vll vector<ll>
#define qll queue<ll>
#define dll deque<ll>
#define pqll priority_queue<ll>
#define gll greater<ll>
#define INF 1000000000000000
#define MOD1 1000000007
#define MOD2 998244353
#define MOD3 1000000009
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
ll n, a[1000005][2];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
n *= 2;
iloop(0, 2) jloop(0, n) cin >> a[j][i];
ll avm[n][2][2];
memset(avm, 0, sizeof(avm));
iloop(0, n) {
if (!i) {
avm[i][0][0] = avm[i][0][1] = 0;
avm[i][1][0] = avm[i][1][1] = 1;
continue;
}
avm[i][0][0] = avm[i][1][0] = INF;
avm[i][0][1] = avm[i][1][1] = -INF;
jloop(0, 2) kloop(0, 2) if (a[i][j] >= a[i-1][k]) {
avm[i][j][0] = min(avm[i][j][0], avm[i-1][k][0] + j);
avm[i][j][1] = max(avm[i][j][1], avm[i-1][k][1] + j);
}
}
ll cc;
if (avm[n-1][0][0] <= n/2 && avm[n-1][0][1] >= n/2) cc = 0;
else if (avm[n-1][1][0] <= n/2 && avm[n-1][1][1] >= n/2) cc = 1;
else {cout << -1; return 0;}
vector<char> v;
ll ccn = 0, cid = n-1;
while (cid >= 0) {
v.push_back((cc ? 'B' : 'A'));
ccn += cc;
if (cid == 0) break;
if (avm[cid-1][0][0] + ccn <= n/2 && avm[cid-1][0][1] + ccn >= n/2 && a[cid-1][0] <= a[cid][cc]) cc = 0;
else cc = 1;
cid--;
}
while (v.size()) {cout << v.back(); v.pop_back();}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |