Submission #212968

#TimeUsernameProblemLanguageResultExecution timeMemory
212968qkxwsmBuilding 4 (JOI20_building4)C++14
100 / 100
367 ms25208 KiB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

const int INF = 1000000007;
const int MAXN = 1000013;

int N;
pii arr[MAXN];
pair<pii, pii> dp[MAXN];
bitset<MAXN> ans;

int32_t main()
{
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(4);
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> N;
    FOR(i, 0, 2 * N)
    {
        cin >> arr[i].fi;
    }
    FOR(i, 0, 2 * N)
    {
        cin >> arr[i].se;
    }
    dp[0] = {{0, 0}, {0, 0}};
    FOR(i, 0, 2 * N)
    {
        dp[i + 1] = {{INF, -INF}, {INF, -INF}};
        if (i == 0 || arr[i - 1].fi <= arr[i].fi)
        {
            ckmin(dp[i + 1].fi.fi, dp[i].fi.fi);
            ckmax(dp[i + 1].fi.se, dp[i].fi.se);
        }
        if (i == 0 || arr[i - 1].se <= arr[i].fi)
        {
            ckmin(dp[i + 1].fi.fi, dp[i].se.fi);
            ckmax(dp[i + 1].fi.se, dp[i].se.se);
        }
        if (i == 0 || arr[i - 1].fi <= arr[i].se)
        {
            ckmin(dp[i + 1].se.fi, dp[i].fi.fi + 1);
            ckmax(dp[i + 1].se.se, dp[i].fi.se + 1);
        }
        if (i == 0 || arr[i - 1].se <= arr[i].se)
        {
            ckmin(dp[i + 1].se.fi, dp[i].se.fi + 1);
            ckmax(dp[i + 1].se.se, dp[i].se.se + 1);
        }
    }
    if ((dp[2 * N].fi.se < N || dp[2 * N].fi.fi > N) && (dp[2 * N].se.se < N || dp[2 * N].se.fi > N))
    {
        cout << "-1\n";
        return 0;
    }
    int rem = N;
    bool dir = (dp[2 * N].se.fi <= N && N <= dp[2 * N].se.se);
    FORD(i, 2 * N, 0)
    {
        if (dir)
        {
            ans[i] = true;
            rem--;
            if (dp[i].fi.fi <= rem && rem <= dp[i].fi.se && (i == 0 || arr[i - 1].fi <= arr[i].se))
            {
                dir = false;
            }
            else
            {
                dir = true;
            }
        }
        else
        {
            ans[i] = false;
            if (dp[i].fi.fi <= rem && rem <= dp[i].fi.se && (i == 0 || arr[i - 1].fi <= arr[i].fi))
            {
                dir = false;
            }
            else
            {
                dir = true;
            }
        }
    }
    FOR(i, 0, 2 * N)
    {
        cout << (ans[i] ? 'B' : 'A');
    }
    cout << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...