Submission #312158

#TimeUsernameProblemLanguageResultExecution timeMemory
312158CodeKrackerBuilding 4 (JOI20_building4)C++14
100 / 100
314 ms75768 KiB
/*input
6
25 18 40 37 29 95 41 53 39 69 61 90
14 18 22 28 18 30 32 32 63 58 71 78
*/
/**
    Author: Kristopher Paul
    Date Created: 30-09-2020
**/
#include<bits/stdc++.h>
#include<stdio.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define int ll
#define pb push_back
#define INF 1e18
//#define MOD 1000000007
#define MOD 998244353
#define mp make_pair
const double PI=3.141592653589793238462643383279502884197169399375105820974944;
#define REP(i,n) for (int i = 0; i < n; i++)
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REPD(i,n) for (int i = n-1; i >= 0; i--)
#define FORD(i,a,b) for (int i = a; i >= b; i--)
#define remax(a,b) a = max(a,b)
#define remin(a,b) a = min(a,b)
#define umap map
#define pii pair<int,int>
#define F first
#define S second
#define mii map<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define itr :: iterator it
#define all(v) v.begin(),v.end()
#define WL(t) while(t--)
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define out(x) cout << #x << " is " << x << endl
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;

//Uncomment for File I/O
//ifstream fin("input.in")

//using namespace __gnu_pbds;

//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds; // set
//typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> pbds; // multiset

int ModExp(int x,int y,int m){
    int res = 1;
    x = x % m;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % m;

        y = y>>1;
        x = (x*x) % m;
    }
    return res;
}

void solve(){
    int n;
    cin >> n;
    n *= 2;
    int a[n],b[n];
    FOR(i,0,n){
        cin >> a[i];
    }
    FOR(i,0,n){
        cin >> b[i];
    }
    int dpmax[n][2];
    int dpmin[n][2];
    //0 -> A, 1 -> B
    dpmax[0][0] = dpmin[0][0] = 1;
    dpmax[0][1] = dpmin[0][1] = 0;
    FOR(i,1,n){
        dpmax[i][0] = -1e18;
        dpmin[i][0] = 1e18;
        if(a[i] >= a[i-1]){
            remax(dpmax[i][0],dpmax[i-1][0]+1);
            remin(dpmin[i][0],dpmin[i-1][0]+1);
        }
        if(a[i] >= b[i-1]){
            remax(dpmax[i][0],dpmax[i-1][1]+1);
            remin(dpmin[i][0],dpmin[i-1][1]+1);
        }
        dpmax[i][1] = -1e18;
        dpmin[i][1] = 1e18;
        if(b[i] >= a[i-1]){
            remax(dpmax[i][1],dpmax[i-1][0]);
            remin(dpmin[i][1],dpmin[i-1][0]);
        }
        if(b[i] >= b[i-1]){
            remax(dpmax[i][1],dpmax[i-1][1]);
            remin(dpmin[i][1],dpmin[i-1][1]);
        }
    }
    int ans[n] = {};
    int numa = (n/2);
    if(dpmin[n-1][0] != 1e18 && dpmax[n-1][0] != -1e18 && dpmin[n-1][0] <= (n/2) && (n/2) <= dpmax[n-1][0]){
        ans[n-1] = 0;
        numa--;
    }else if(dpmin[n-1][1] != 1e18 && dpmax[n-1][1] != -1e18 && dpmin[n-1][1] <= (n/2) && (n/2) <= dpmax[n-1][1]){
        ans[n-1] = 1;
    }else{
        cout << "-1" << endl;
        return;
    }
    FORD(i,n-2,0){
        int val;
        if(ans[i+1] == 0){
            val = a[i+1];
        }else{
            val = b[i+1];
        }
        bool c1 = false,c2 = false;
        if(dpmin[i][0] != 1e18 && dpmax[i][0] != -1e18){
            if(dpmin[i][0] <= numa && numa <= dpmax[i][0] && a[i] <= val){
                ans[i] = 0;
                numa--;
                c1 = true;
            }
        }
        if(c1 == false && dpmin[i][1] != 1e18 && dpmax[i][1] != -1e18){
            if(dpmin[i][1] <= numa && numa <= dpmax[i][1] && b[i] <= val){
                ans[i] = 1;
                c2 = true;
            }
        }
        if(c1 == false and c2 == false){
            cout << "-1" << endl;
            return;
        }
    }
    FOR(i,0,n){
        (ans[i])?cout << "B":cout << "A";
    }
    cout << endl;
}

signed main(){
    FastIO;
    int t = 1;
//    cin >> t;
    WL(t){
        solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...