Submission #1099538

#TimeUsernameProblemLanguageResultExecution timeMemory
1099538vladilius건물 4 (JOI20_building4)C++17
11 / 100
382 ms524288 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];
    
    bool dp[m + 1][n + 1][2];
    for (int i = 1; i <= m; i++){
        for (int j = 1; j <= n; j++){
            dp[i][j][0] = dp[i][j][1] = 0;
        }
    }
    
    dp[1][1][0] = dp[1][0][1] = 1;
    
    for (int i = 2; i <= m; i++){
        for (int j = 0; j <= n; j++){
            if (j > 0 && a[i - 1] <= a[i]) dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j - 1][0]);
            if (j > 0 && b[i - 1] <= a[i]) dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j - 1][1]);
            if (a[i - 1] <= b[i]) dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][0]);
            if (b[i - 1] <= b[i]) dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][1]);
        }
    }

    if (dp[m][n][0] || dp[m][n][1]){
        vector<bool> all;
        int i = m, j = n, c = (dp[m][n][0]) ? 0 : 1;
        while (true){
            all.pb(c);
            if (i == 1) break;
            if (c){
                if (a[i - 1] <= b[i] && dp[i - 1][j][0]){
                    c = 0;
                }
            }
            else {
                if (b[i - 1] <= a[i] && dp[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...