Submission #1368629

#TimeUsernameProblemLanguageResultExecution timeMemory
1368629peyman_sfBuilding 4 (JOI20_building4)C++20
100 / 100
140 ms49380 KiB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;

const ll maxn = 3e6;
const ll mod = 1e9 + 7;
const ll inf = 2e18;

#define TextIO ifstream fileIn("input.txt"); cin.rdbuf(fileIn.rdbuf()); ofstream fileOut("output.txt"); cout.rdbuf(fileOut.rdbuf());
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define print(x) for (auto i : x) cout << i << ' '; cout << endl
#define out(x) {cout << x << endl; return;}
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define endl '\n'

ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b);}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll pw(ll a, ll b, ll md = mod) {ll res = 1; b = max(b, 0ll); while(b) {if (b & 1){res = (a * res) % md;} a = (a * a) % md; b >>= 1;} return (res % md);}

ll n;
ll a[maxn][2];
pll dp[maxn][2];

void solve() {
    cin >> n;
    for (ll i = 1; i <= 2 * n; i++) cin >> a[i][0];
    for (ll i = 1; i <= 2 * n; i++) cin >> a[i][1];

    for (ll i = 1; i <= 2 * n; i++) {
        for (ll j : {0, 1}) {
            dp[i][j] = {n + 1, -1};
            for (ll k : {0, 1}) if (a[i - 1][k] <= a[i][j]) {
                dp[i][j].first = min(dp[i][j].first, dp[i - 1][k].first + j);
                dp[i][j].second = max(dp[i][j].second, dp[i - 1][k].second + j);
            }
        }
    }

    ll ok = 0, j = 0;
    if (dp[2 * n][0].first <= n && dp[2 * n][0].second >= n) ok = 1;
    if (dp[2 * n][1].first <= n && dp[2 * n][1].second >= n) ok = 1, j = 1;
    
    if (!ok) out(-1);
    string ans(2 * n, 'A');
    ll x = n;
    for (ll i = 2 * n; i > 0; i--) {
        ans[i - 1] = (j ? 'B' : 'A');
        x -= j;
        j = (a[i - 1][0] <= a[i][j] && dp[i - 1][0].first <= x && dp[i - 1][0].second >= x ? 0 : 1);
    }
    cout << ans << endl;
}

int main() {
    FastIO
    ll t = 1;
    // cin >> t;
    while (t--)
        solve();

    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...