Submission #1228372

#TimeUsernameProblemLanguageResultExecution timeMemory
1228372ssafarovBuilding 4 (JOI20_building4)C++20
0 / 100
1 ms584 KiB
#define Magic ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long long double
#define en '\n'
#define tsts int tetss; cin >> tetss; while(tetss--)
#define all(a) a.begin() , a.end()
#define pb push_back
#define ld long long double
#define fi first
#define se second
ll INF = 1e18;
const int N = 4e5 + 1;
const int mod = 998244353;
using namespace std;
using namespace __gnu_pbds;
 
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; 
 
// order_of_key(n) - The number of items in a set that are strictly smaller than k
// find_by_order(k) - It returns an iterator to the ith largest element
 
ll lcm(ll a, ll b)
{
  ll gc = __gcd(a, b);
  return   a / gc * b;
}
 
ll binpow (ll a, ll n) {
  if (n == 0)
    return 1;
  if (n % 2 == 1)
    return binpow (a, n-1) * a;
  else {
    ll b = binpow (a, n/2);
    return b * b;
  }
}
 
 
ll binpow_mod(ll a, ll b, ll md)
{
    ll res = 1;
    a = a % md;
    while (b > 0)
    {
        if (b & 1)
            res = (res * a) % md;
        a = (a * a) % md;
        b >>= 1;
    }
    return res;
}
 
ll in() {ll x; cin >> x; return x;};
 
ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
		x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
	x = y1 - (b / a) * x1;
	y = x1;
	return d;
}
 
ll gcdinv(ll a, ll b){
	ll x, y;
	gcd(a, b, x, y);
	return x;
}
 
ll eql(ll x, ll y, bool ok){
	if(ok) return x;
	else return y;
}
 
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- чё

ll dp[1000012][2];
ll wh[1000012][2];

void solve(){
	ll n; cin >> n;
	ll a[2 * n + 12][2];
	for(ll i = 1; i <= n * 2; ++i){
		for(ll j = 0; j < 2; ++j){
			dp[i][j] = INF;
		}
	}
	for(ll i = 1; i <= 2 * n; ++i) cin >> a[i][0];
	for(ll i = 1; i <= 2 * n; ++i) cin >> a[i][1];
	dp[1][0] = 1;
	dp[1][1] = -1;
	for(ll i = 2; i <= 2 * n; ++i){
		for(ll x = 0; x < 2; ++x){
			for(ll y = 0; y < 2; ++y){
				ll ch1 = a[i][x];
				ll ch2 = a[i - 1][y];
				if(ch2 > ch1){
					continue;
				}
				ll nw = dp[i - 1][y];
				if(x == 0) nw++;
				else nw--;
				if(abs(nw) < abs(dp[i][x])){
					dp[i][x] = nw;
					wh[i][x] = y;
				}
			}
		}
	}
	if(dp[2 * n][0] == 0){
		deque<ll> dq;
		ll cur = 0;
		for(ll i = 2 * n; i; --i){
			dq.push_front(cur);
			cur = wh[i][cur];
		}
		for(auto g : dq){
			if(g == 0) cout << 'A';
			else cout << 'B';
		}
		return;
	}
	if(dp[2 * n][1] == 0){
		deque<ll> dq;
		ll cur = 1;
		for(ll i = 2 * n; i; --i){
			dq.push_front(cur);
			cur = wh[i][cur];
		}
		for(auto g : dq){
			if(g == 0) cout << 'A';
			else cout << 'B';
		}
		return;
	}
	cout << -1;
}

int main(){
	// freopen("fairphoto.in", "r", stdin); freopen("fairphoto.out", "w", stdout);
	Magic
	// tsts{
		solve();
		cout << endl;
	// }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...