This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 1e6 + 7;
int a[2][N];
ii dp[N][2];
void merge(ii &a, ii b) {
if (a.s < a.f)
a = b;
else
a = mp(min(a.f, b.f), max(a.s, b.s));
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n;
cin >> n;
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2 * n; ++j)
cin >> a[i][j];
for (int i = 0; i < N; ++i)
for (int j = 0; j < 2; ++j)
dp[i][j] = mp(N, -N);
dp[0][0] = mp(0, 0);
for (int i = 0; i < 2 * n; ++i) {
for (int t = 0; t < 2; ++t) {
int cur = -1;
if (i)
cur = a[t][i - 1];
for (int add = 0; add < 2; ++add) {
if (cur <= a[add][i]) {
merge(dp[i + 1][add], mp(dp[i][t].f + (add == 0), dp[i][t].s + (add == 0)));
}
}
}
}
bool r = 0;
for (int i = 0; i < 2; ++i)
if (dp[2 * n][i].f <= n && n <= dp[2 * n][i].s)
r = i;
if (n < dp[2 * n][r].f || n > dp[2 * n][r].s) {
cout << "-1" << endl;
}
else {
string ans;
int cnt = n;
for (int i = 2 * n; ; --i) {
if (r)
ans += 'B';
else {
--cnt;
ans += 'A';
}
if (i == 1)
break;
if (a[0][i - 2] <= a[r][i - 1] && dp[i - 1][0].f <= cnt && cnt <= dp[i - 1][0].s)
r = 0;
else
r = 1;
}
reverse(all(ans));
cout << ans << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |