This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;
using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;
using db = long double;
template<class T> using pq = priority_queue<T, V<T>, less<T>>;
const int MOD = 1e9 + 7;
const ll INFL = ll(1e17);
const int LG = 19;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N; cin >> N; N *= 2;
V<vi> A(2, vi(N)); for(int r = 0; r < 2; r++) for(auto& x : A[r]) cin >> x;
V<V<vpi>> par(2, V<vpi>(2, vpi(N, mp(-MOD, -MOD))));
V<vi> mn(2, vi(N, MOD)), mx(2, vi(N, -MOD));
mn[0][0] = mx[0][0] = 1; par[0][0][0] = mp(0, 0);
mn[1][0] = mx[1][0] = 0; par[1][0][0] = mp(0, 0);
for(int c = 0; c < N-1; c++) {
for(int r = 0; r < 2; r++) {
for(int R = 0; R < 2; R++) {
if (A[r][c] <= A[R][c+1]) {
mn[R][c+1] = min(mn[r][c] + !R, mn[R][c+1]);
mx[R][c+1] = max(mx[r][c] + !R, mx[R][c+1]);
par[R][r][c+1] = mp(mn[r][c], mx[r][c]);
}
}
}
}
for(int sr = 0; sr < 2; sr++) {
if (mn[sr].back() <= N / 2 && N / 2 <= mx[sr].back()) {
int cur = N / 2; int R = sr;
string ans; bool ok = 1;
for(int C = N - 1; C >= 0; C--) {
ans += char('A' + R);
bool done = 0;
for(int nr = 0; nr < 2; nr++) {
int lx, rx; tie(lx, rx) = par[R][nr][C];
int ncur = cur - !R;
if (lx <= ncur && ncur <= rx) {
cur = ncur, R = nr; done = 1; break;
}
}
ok &= done;
if (!ok) break;
}
if (ok) {
reverse(begin(ans), end(ans));
cout << ans << nl;
exit(0-0);
}
}
}
cout << -1 << nl;
exit(0-0);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |