Submission #1192998

#TimeUsernameProblemLanguageResultExecution timeMemory
1192998hengliaoBuilding 4 (JOI20_building4)C++20
11 / 100
110 ms16112 KiB
#include<bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define pll pair<ll, ll>
#define vll vector<ll>
#define pb push_back

typedef long long ll;

const ll mxN=4005;
const ll inf=1e18;

array<ll, 2> dp[mxN][2];

array<ll, 2> mrg(array<ll, 2> a, array<ll, 2> b){
    if(a[0]==-1){
        return b;
    }
    return {min(a[0], b[0]), max(a[1], b[1])};
}

void solve(){
    ll n;
    cin>>n;
    vll a(2*n);
    for(auto &it:a){
        cin>>it;
    }
    vll b(2*n);
    for(auto &it:b){
        cin>>it;
    }
    for(ll i=0;i<2*n;i++){
        for(ll j=0;j<2;j++){
            dp[i][j]={-1, -1};
        }
        
    }
    dp[0][0]={1, 1};
    dp[0][1]={0, 0};
    for(ll i=1;i<2*n;i++){
        for(ll j=0;j<2;j++){
            set<array<ll, 2>> st;
            // st.insert(dp[i][j]);
            if(j==0){
                if(a[i]>=a[i-1]){
                    if(dp[i-1][0][0]!=-1){
                        st.insert({dp[i-1][0][0]+1, dp[i-1][0][1]+1});
                    }
                }
                if(a[i]>=b[i-1]){
                    if(dp[i-1][1][0]!=-1){
                        st.insert({dp[i-1][1][0]+1, dp[i-1][1][1]+1});
                    }
                }
            }
            else{
                if(b[i]>=a[i-1]){
                    if(dp[i-1][0][0]!=-1){
                        st.insert(dp[i-1][0]);
                    }
                }
                if(b[i]>=b[i-1]){
                    if(dp[i-1][1][0]!=-1){
                        st.insert(dp[i-1][1]);
                    }
                }
            }
            for(auto &it:st){
                dp[i][j]=mrg(dp[i][j], it);
            }
            // cout<<i<<' '<<j<<' '<<dp[i][j][0]<<' '<<dp[i][j][1]<<'\n';
        }
    }
    // if(n>=dp[2*n-1][0][0] && n<=dp[2*n-1][0][1]){
    ll cur=n;
    ll pt=2*n-1;
    vll ans;
    ll pre=inf;
    while(pt>=0){
        if(a[pt]<=pre && cur>=dp[pt][0][0] && cur<=dp[pt][0][1]){
            ans.pb(0);
            cur--;
            pre=a[pt];
        }
        else if(b[pt]<=pre && cur>=dp[pt][1][0] && cur<=dp[pt][1][1]){
            ans.pb(1);
            pre=b[pt];
        }
        else{
            break;
        }
        pt--;
    }
    if((ll) ans.size()==0){
        cout<<-1<<'\n';
        return;
    }
    reverse(ans.begin(), ans.end());
    for(auto &it:ans){
        if(it==0){
            cout<<"A";
        }
        else{
            cout<<"B";
        }
    }
    cout<<'\n';
    // }
}

int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();

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