Submission #1169880

#TimeUsernameProblemLanguageResultExecution timeMemory
1169880monaxiaBuilding 4 (JOI20_building4)C++20
11 / 100
360 ms589824 KiB
#include <bits/stdc++.h>
#define pb push_back
#define ppb pop_back
#define fr first
#define sc second
#define all(v) v.begin(), v.end()
#define mod (long long)(1e9 + 7)
#define eps (long long)(1e-9)
#define vektor vector
using namespace std;
 
using ll = long long;
using ull = unsigned long long;
using ld = long double;
 
const ull Mod = 998244353;
const ll LIMIT = 2e5;

void solve(){
    int n;
    cin >> n;

    int m = n;
    n *= 2;

    vector <int> a(n + 1), b(n + 1);

    for(int i = 1; i <= n; i ++) cin >> a[i];
    for(int i = 1; i <= n; i ++) cin >> b[i];

    int dp[n + 1][m + 2][2];
    
    for(int i = 1; i <= n; i ++){
        for(int j = 0; j <= m; j ++) dp[i][j][0] = dp[i][j][1] = 0;
    }

    dp[0][0][0] = 1;
    dp[0][0][1] = 1;

    for(int i = 1; i <= n; i ++){
        for(int j = max(0, i - m); j <= min(i, m); j ++){
            if(j > 0) dp[i][j][0] = (a[i - 1] <= a[i] && dp[i - 1][j - 1][0]) || (b[i - 1] <= a[i] && dp[i - 1][j - 1][1]);
            if(j < i) dp[i][j][1] = (a[i - 1] <= b[i] && dp[i - 1][j][0]) || (b[i - 1] <= b[i] && dp[i - 1][j][1]);      

            // cout << i << " " << j << ":\n";

            // if(j > 0) cout << dp[i][j][0];
            // else cout << " ";
            // cout << " ";
            // if(j < i) cout << dp[i][j][1];

            // cout << "\n\n";
        }
    }

    if(!dp[n][m][0] && !dp[n][m][1]){
        cout << -1;
        return;
    }

    string ans = "";

    int i = n, j = m, k;

    if(dp[n][m][0]) ans.pb('A'), k = 0;
    else ans.pb('B'), k = 1;

    while(i > 1){
        // cout << i << " " << j << " " << k << "\n";
        // if(k) cout << a[i - 1] << ' '
        if(!k){
            if(b[i - 1] <= a[i] && dp[i - 1][j - 1][1]) k = 1;
            j --;
        }

        else if(a[i - 1] <= b[i] && dp[i - 1][j][0]) k = 0;

        i --;

        if(k) ans.pb('B');
        else ans.pb('A');
    } 

    reverse(all(ans));

    cout << ans;
}

 
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
 
    if(fopen("nameholder.inp", "r")){
        freopen("nameholder.inp", "r", stdin);
        freopen("nameholder.out", "w", stdout);
    }
    
    // cout << 1; return 0;
 
    ll n = 1;
 
    // cin >> n;
 
    while(n) {
        solve();
        n --;
        cout << "\n";
    }
 
    // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    return 0;
}
 
//                                   ++++                                           
//                          ++++-::......:--:::-=+++                                
//                    ++++=:............---.....--:..:=++++++                       
//                    +++++++::......................::=+++++                       
//             @@               ++++++++++:+++++++++                                
//             @%                                  @*                               
//              %%@@                    *****#*@@                                   
//             %%%%%                  @##**##***%                                   
//           @@@@@@+-*#*@             #####%+*##*@                                  
//                  %#+@%%@@@         ##-%:::-#*###@@                               
//                 =====:*:::        ###-:==::=#*%*                                 
//                    -====:::::+@@%@@%@#*--=*#%%#@%@   @%@                         
//                        @=====%=-@%=:::.:+-::::+::::::::::::%-----%% @@@@@        
//                        @@%#*@++%===+::-================@%@@@@+++++++@@@%%%@#%###%
//               @#***#@@   @##=%@===+--:+==+===+%*##@%%%@@@@@@@@@@:=  @%@%%%@#%%   
//    -@      @*#@         #####%:::-====+:::==---**%#@ *%%%%@@@@@@-:               
//    -=-*%  #          #***--%%::::::::+=-+#-----=**@*#     #+**#@#                
//     ----=----#+****=----=*#@=::::::=#-----------***%*%                           
//      ------------%*****##=@==::::::+%-----------***%**                           
//       +-----------------*@++%::::::::-----------****@*#                          
//         =------------% @%=+%#+:::::::* #-----*--**** **                          
//            ----%--%    *==+++++=+=##*:%   ---=--**** **                          
//                       %@@@@@@@@@==@+%%%%@   ----**** *#                          
//                     @=*+%@@@%@@=++=:%:::%      #***@@#                           
//                    #+++++++++#==++-::::::     @**** *#                           
//                   =+++++++++++==+++:::::::   ####@ #%                            
//                  +++++++++++=@ @++++::::::@      @#                              
//                 @+++++++++=@    =*++=:::::@                                      
//                 ++++++++@       @++++:::::                                       
//                ++++++=           =++++:::                                        
//               =+++++@             =+++::::                                       
//             ++++++-@               =+++:+::                                      
//           @::+::::@                 =++::-:::                                    
//          +::=:::::                  @+*+:-+++-:                                  
//        @::+-:::::#                     +++::::::@                                
//       ::%=:::::::%@                    @%++::::***                               
//     @:+@*:::::::****@                   +@+++:-=*:*@                             
//    *:@@%::::::::=****                   @%@+++:::#**@                            
//   *%@@@-:::::::%=+==*@                   +@%+++:***+*                            
//  #%@@%%::::::::#+=-=#                    =+@+++++:::*+                           
// @####+::::::::%+=@#%#=@                  @**@@%=++::::@                          
// @   +:::::::::***#*@ @@###+=+=%@          +**#+=+*+:::-                          
//    =-:::::::%%%%@@          @#=*########%#+*%%+--@*+::%                          
//   +---%@                                   @@ =-:-**:#                           
//  %%%%%                                        @+--*@                             
// @%%%%@                                        @%%%%%                             
//                                                @%%@@

Compilation message (stderr)

building4.cpp: In function 'int main()':
building4.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |         freopen("nameholder.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
building4.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |         freopen("nameholder.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...