This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int mxN = 5e5 + 5;
ll n,a[mxN],b[mxN];
pair<ll,ll> A[mxN],B[mxN];
char ans[mxN];
void rec(ll i, ll k, ll x){
if(i == 0) return;
// cout << i << ' ' << ((x == 0) ? a[i] : b[i]) << ' ' << a[i - 1] << ' ' << x << '\n';
if(A[i - 1].first <= k and A[i - 1].second >= k and a[i - 1] <= ((x == 0) ? a[i] : b[i])){
ans[i - 1] = 'A';
rec(i - 1, k - 1, 0);
}
else{
ans[i - 1] = 'B';
rec(i - 1, k, 1);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 0; i < 2 * n; i++) cin >> a[i];
for(int i = 0; i < 2 * n; i++) cin >> b[i];
A[0] = {1, 1};
B[0] = {0, 0};
for(int i = 1; i < 2 * n; i++){
A[i].first = 1e9;
A[i].second = -1;
if(a[i] >= a[i - 1]) A[i] = {A[i - 1].first + 1, A[i - 1].second + 1};
if(a[i] >= b[i - 1]) A[i] = {min(A[i].first, B[i - 1].first + 1), max(A[i].second, B[i - 1].second + 1)};
B[i].first = 1e9;
B[i].second = -1;
if(b[i] >= b[i - 1]) B[i] = {B[i - 1].first, B[i - 1].second};
if(b[i] >= a[i - 1]) B[i] = {min(B[i].first, A[i - 1].first), max(B[i].second, A[i - 1].second)};
if(A[i].second == -1 and B[i].second == -1){
cout << -1;
return 0;
}
// cout << A[i].first << ' ' << A[i].second << ' ' << B[i].first << ' ' << B[i].second << '\n';
}
if(A[2 * n - 1].first <= n and A[2 * n - 1].second >= n){
ans[2 * n - 1] = 'A';
rec(2 * n - 1, n - 1, 0);
for(int i = 0; i < 2 * n; i++) cout << ans[i];
}
else if(B[2 * n - 1].first <= n and B[2 * n - 1].second >= n){
ans[2 * n - 1] = 'B';
rec(2 * n - 1, n, 1);
for(int i = 0; i < 2 * n; i++) cout << ans[i];
}
else cout << -1;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |