Submission #1040041

#TimeUsernameProblemLanguageResultExecution timeMemory
1040041otariusBuilding 4 (JOI20_building4)C++17
100 / 100
175 ms48212 KiB
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ull unsigned long long
#define all(x) (x).begin(),(x).end()

// #define int long long
// #define int unsigned long long

// #define ordered_set(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
// #define ordered_multiset(T) tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>

void open_file(string filename) {
    freopen((filename + ".in").c_str(), "r", stdin);
    freopen((filename + ".out").c_str(), "w", stdout);
}

// const ll mod = 1e9 + 7;
// const ll mod = 998244353;

const ll inf = 1e9;
const ll biginf = 1e18;
const int maxN = 5 * 1e5 + 25;

int a[2 * maxN][2], dp[2 * maxN][2][2];
void solve() {
    int n;
    cin >> n;
    for (int j = 0; j < 2; j++)
        for (int i = 1; i <= 2 * n; i++) cin >> a[i][j];
    // dp[i][0][0] - minimal 'A' when i-th is 'A'
    // dp[i][0][1] - maximal 'A' when i-th is 'A'
    // dp[i][1][0] - minimal 'A' when i-th is 'B'
    // dp[i][1][1] - maximal 'A' when i-th is 'B'
    dp[1][0][0] = dp[1][0][1] = 1;
    for (int i = 2; i <= 2 * n; i++) {
        for (int j = 0; j < 2; j++) {
            dp[i][j][0] = inf; dp[i][j][1] = -inf;
            for (int k = 0; k < 2; k++) {
                if (a[i][j] >= a[i - 1][k]) {
                    dp[i][j][0] = min(dp[i][j][0], dp[i - 1][k][0] + (j == 0));
                    dp[i][j][1] = max(dp[i][j][1], dp[i - 1][k][1] + (j == 0));
                }
            }
        }
    } int ans[2 * n + 1];
    for (int _ = 0; _ < 2; _++) {
        if (dp[2 * n][_][0] <= n && n <= dp[2 * n][_][1]) {
            int cur = _, cnt = 0;
            for (int i = 2 * n; i >= 1; i--) {
                ans[i] = cur; cnt += (cur == 0);
                if (dp[i - 1][0][0] + cnt <= n && n <= dp[i - 1][0][1] + cnt && a[i][cur] >= a[i - 1][0]) cur = 0; else cur = 1;
            } for (int i = 1; i <= 2 * n; i++) {
                if (ans[i]) cout << 'B';
                else cout << 'A';
            } return;
        }
    } cout << -1;
}
int32_t main() { 
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        cout << '\n';
    }
    return 0;
}

Compilation message (stderr)

building4.cpp: In function 'void open_file(std::string)':
building4.cpp:26:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |     freopen((filename + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
building4.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     freopen((filename + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...