#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 1e6 + 5;
const int inf = 1e9 + 7;
const long long INF = (long long)1e18 + 7;
const int mod = 1e9 + 7;
void add(int &x, int y) {x += y; if (x >= mod) x -= mod;}
void sub(int &x, int y) {x -= y; if (x < 0) x += mod;}
int mul(int x, int y) {return 1LL * x * y % mod;}
// -----------------------------------------//
int n;
int a[N][2];
pair<int, int> dp[N][2];
void read() {
cin >> n;
for(int i = 1; i <= 2 * n; i++) cin >> a[i][0];
for(int i = 1; i <= 2 * n; i++) cin >> a[i][1];
}
pair<int, int> merge(const ii &x, const ii &y) {
return {min(x.fi, y.fi), max(x.se, y.se)};
}
bool in(int x, ii y) {
return y.fi <= x && x <= y.se;
}
void solve() {
dp[1][1] = {1, 1}; // type B: +1
dp[1][0] = {0, 0}; // type A: +0
for(int i = 2; i <= 2 * n; i++) {
for(int j = 0; j < 2; j++) {
dp[i][j] = {inf, -inf};
for(int k = 0; k < 2; k++) {
if (a[i][j] >= a[i - 1][k]) {
dp[i][j] = merge(dp[i][j], mp(dp[i - 1][k].fi + (j == 1), dp[i - 1][k].se + (j == 1)));
}
}
}
}
// cerr << dp[2 * n][1].fi << ' ' << dp[2 * n][1].se << endl;
if ((dp[2 * n][0].se < n || dp[2 * n][0].fi > n) &&
(dp[2 * n][1].se < n || dp[2 * n][1].fi > n)) {
cout << -1 << endl;
return;
}
vector<char> ans;
int cntB = 0;
int pre = inf;
for(int i = 2 * n; i >= 1; i--) {
if (in(n - cntB, dp[i][0]) && a[i][0] <= pre) {
ans.push_back('A');
pre = a[i][0];
}
else {
ans.push_back('B');
++cntB;
pre = a[i][1];
}
}
reverse(ALL(ans));
for(char c: ans) cout << c;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int test = 1;
// cin >> test;
while(test--) {
read();
solve();
}
return 0;
}
Compilation message (stderr)
building4.cpp: In function 'int main()':
building4.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
building4.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |