Submission #659423

#TimeUsernameProblemLanguageResultExecution timeMemory
659423Ronin13Building 4 (JOI20_building4)C++14
100 / 100
747 ms45376 KiB
#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;

pii merg(pii a, pii b){
    return {min(a.f, b.f), max(a.s, b.s)};
}

int main(){
    #ifndef ONLINE_JUDGE
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    #endif // ONLINE_JUDGE
    int n; cin >> n;
    int a[2 * n + 1][2];
    for(int j = 0; j < 2; j++){
        for(int i = 1; i <= 2 * n; i++)
            cin >> a[i][j];
    }
    pii dp[2 * n + 1][2];
    dp[1][0] = {0, 0};
    dp[1][1] = {1, 1};
    for(int i  = 2; i <= 2 * n; i++){
        for(int j = 0; j < 2; j++){
            pii x = {1e9, -1e9};
            pii y = {1e9, -1e9};
            if(a[i][j] >= a[i - 1][0]){
                x = {dp[i - 1][0].f + j, dp[i - 1][0].s + j};
            }
            if(a[i][j] >= a[i - 1][1]){
                y = {dp[i - 1][1].f + j, dp[i - 1][1].s + j};
            }
            dp[i][j] = merg(x, y);
        }
    }
    /*for(int i = 1; i <= 2 * n; i++){
        for(int j = 0; j < 2; j++){
            cout << dp[i][j].f << ' ' << dp[i][j].s << " ";
        }
        cout << "\n";
    }*/
    int last;
    int cur = n;
    string ans = "";
    if(dp[2 * n][0].f > n || dp[2 * n][0].s < n){
        if(dp[2 * n][1].f > n || dp[2 * n][1].s < n){
            cout << -1 << "\n";
            return 0;
        }
        last = 1;
        cur--;
        ans += 'B';
    }
    else{
        last = 0;
        ans += 'A';
    }
    for(int i = 2 * n - 1; i >= 1; i--){
        for(int j = 0; j < 2; j++){
            if(a[i][j] > a[i + 1][last]) continue;
            if(dp[i][j].f <= cur && dp[i][j].s >= cur){
                last = j;
                cur -= j;
                break;
            }
        }
        if(last) ans += 'B';
        else ans += 'A';
    }
    reverse(ans.begin(), ans.end());
    cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...