Submission #1099580

#TimeUsernameProblemLanguageResultExecution timeMemory
1099580vladiliusBuilding 4 (JOI20_building4)C++17
100 / 100
187 ms44612 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n; cin>>n;
    int m = 2 * n;
    vector<int> a(m + 1), b(m + 1);
    for (int i = 1; i <= m; i++) cin>>a[i];
    for (int i = 1; i <= m; i++) cin>>b[i];
    
    pii rr[m + 1][2];
    rr[1][0] = {1, 1};
    rr[1][1] = {0, 0};
    
    const int inf = 1e9;
    
    int l1, r1;
    for (int i = 2; i <= m; i++){
        l1 = inf; r1 = 0;
        if (a[i - 1] <= a[i] && rr[i - 1][0].ff != -1){
            l1 = min(l1, rr[i - 1][0].ff + 1);
            r1 = max(r1, rr[i - 1][0].ss + 1);
        }
        if (b[i - 1] <= a[i] && rr[i - 1][1].ff != -1){
            l1 = min(l1, rr[i - 1][1].ff + 1);
            r1 = max(r1, rr[i - 1][1].ss + 1);
        }
        if (l1 <= r1) rr[i][0] = {l1, r1};
        else rr[i][0] = {-1, -1};
        
        l1 = inf; r1 = 0;
        if (a[i - 1] <= b[i] && rr[i - 1][0].ff != -1){
            l1 = min(l1, rr[i - 1][0].ff);
            r1 = max(r1, rr[i - 1][0].ss);
        }
        if (b[i - 1] <= b[i] && rr[i - 1][1].ff != -1){
            l1 = min(l1, rr[i - 1][1].ff);
            r1 = max(r1, rr[i - 1][1].ss);
        }
        if (l1 <= r1) rr[i][1] = {l1, r1};
        else rr[i][1] = {-1, -1};
    }
    
    auto get = [&](int x, int y, int b){
        return (rr[x][b].ff <= y && y <= rr[x][b].ss);
    };
    
    if (get(m, n, 0) || get(m, n, 1)){
        vector<bool> all;
        int i = m, j = n, c = get(m, n, 0) ? 0 : 1;
        while (true){
            all.pb(c);
            if (i == 1) break;
            if (c){
                if (a[i - 1] <= b[i] && get(i - 1, j, 0)){
                    c = 0;
                }
            }
            else {
                if (b[i - 1] <= a[i] && get(i - 1, j - 1, 1)){
                    c = 1;
                }
                j--;
            }
            i--;
        }
        reverse(all.begin(), all.end());
        for (bool i: all) cout<<((i) ? "B" : "A");
        cout<<"\n";
    }
    else {
        cout<<-1<<"\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...