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 all(x) (x).begin(), (x).end()
#define sz(x) ( (int)(x).size() )
using LL = long long;
template<class T>
inline bool asMn(T &a, const T &b) { return a > b ? a = b, true : false; }
template<class T>
inline bool asMx(T &a, const T &b) { return a < b ? a = b, true : false; }
mt19937 rng( (uint32_t)chrono::steady_clock::now().time_since_epoch().count() );
pair<int, int> operator + (pair<int, int> &a, int b) { return { a.first + b, a.second + b }; }
void operator += (pair<int, int> &a, pair<int, int> b) {
if (a == pair<int, int>{ -1, -1 } ) a = b;
else {
asMn(a.first, b.first);
asMx(a.second, b.second);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef FourLeafClover
freopen("input", "r", stdin);
#endif // FourLeafCLover
int n; cin >> n;
vector<int> a(n << 1), b(n << 1);
for (int i = 0; i < n << 1; ++i) cin >> a[i];
for (int i = 0; i < n << 1; ++i) cin >> b[i];
vector<array<pair<int, int>, 2> > f(n << 1, { pair<int, int>{ -1, -1 }, pair<int, int>{ -1, -1 } });
f[0] = { pair<int, int>{ 1, 1 }, pair<int, int>{ 0, 0 } };
for (int i = 1; i < n << 1; ++i) {
for (int j = 0; j < 2; ++j) if (~f[i - 1][j].first) {
for (int k = 0; k < 2; ++k) if ( (j ? b[i - 1] : a[i - 1]) <= (k ? b[i] : a[i]) ) {
f[i][k] += f[i - 1][j] + !k;
}
}
// for (int j = 0; j < 2; ++j) cerr << f[i][j].first << '-' << f[i][j].second << ' ';
// cerr << '\n';
}
int curJ = -1;
for (int j = 0; j < 2; ++j) if (f.back()[j].first <= n && f.back()[j].second >= n) curJ = j;
if (!~curJ) return cout << "-1\n", 0;
string ans(n << 1, ' ');
for (int i = (n << 1) - 1, cur = n; i; --i) {
// cerr << "i: " << i << " curJ: " << curJ << '\n';
ans[i] = curJ ? 'B' : 'A';
cur = cur + (!curJ ? -1 : 0);
for (int nxtJ = 0; nxtJ < 2; ++nxtJ) if (~f[i - 1][nxtJ].first
&& (curJ ? b[i] : a[i]) >= (nxtJ ? b[i - 1] : a[i - 1])
&& f[i - 1][nxtJ].first <= cur && cur <= f[i - 1][nxtJ].second) {
curJ = nxtJ;
break ;
}
}
ans[0] = curJ ? 'B' : 'A';
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |