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;
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
#define int long long
#define ll __int128
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
ll read() { // read int128
int x; cin >> x; return (ll)x;
}
long long bm(long long b, long long p) {
if(p==0) return 1 % MOD;
long long r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
long long inv(long long b) {
return bm(b, MOD-2);
}
long long f[MAXN];
long long nCr(int n, int r) {
long long ans = f[n]; ans *= inv(f[r]); ans %= MOD;
ans *= inv(f[n-r]); ans %= MOD; return ans;
}
void precomp() {
for(int i=0; i<MAXN; i++) f[i] = (i == 0 ? 1 % MOD : (f[i-1] * i) % MOD);
}
pair<int, int> seg_union(vector<pair<int,int> > v) {
int mi = 1e9, ma = -1e9;
for(auto x: v) mi = min(mi, x.first), ma = max(ma, x.second);
if(ma == -1e9) return {-1, -1};
return {mi, ma};
}
void solve(int tc) {
int n;
cin >> n;
int a[2*n + 1], b[2*n + 1];
for(int i=1; i<=2*n; i++) cin >> a[i];
for(int i=1; i<=2*n; i++) cin >> b[i];
int la[2*n + 1], ra[2*n + 1], lb[2*n + 1], rb[2*n + 1];
for(int i=1; i<=2*n; i++) la[i] = ra[i] = lb[i] = rb[i] = -1;
la[1] = ra[1] = 1;
lb[1] = rb[1] = 0;
for(int i=2; i<=2*n; i++) {
vector<pair<int, int> > A, B;
if(a[i-1] <= a[i] && la[i-1] != -1) {
A.push_back({la[i-1] + 1, ra[i-1] + 1});
}
if(b[i-1] <= a[i] && lb[i-1] != -1) {
A.push_back({lb[i-1] + 1, rb[i-1] + 1});
}
if(a[i-1] <= b[i] && la[i-1] != -1) {
B.push_back({la[i-1], ra[i-1]});
}
if(b[i-1] <= b[i] && lb[i-1] != -1) {
B.push_back({lb[i-1], rb[i-1]});
}
pair<int, int> resa = seg_union(A);
pair<int, int> resb = seg_union(B);
la[i] = resa.first, ra[i] = resa.second;
lb[i] = resb.first, rb[i] = resb.second;
}
if(la[2*n] <= n && n <= ra[2*n]) {
stack<char> st;
char cur = 'A';
int tar = n;
for(int i=2*n; i>=1; i--) {
st.push(cur);
// find tar!!
if(cur == 'A') tar--;
if(la[i-1] <= tar && tar <= ra[i-1] && a[i-1] <= (cur == 'A' ? a[i] : b[i])) cur = 'A';
else cur = 'B';
}
while(st.size()) {
cout << st.top();
st.pop();
}
cout << '\n';
}
else if(lb[2*n] <= n && n <= rb[2*n]) {
stack<char> st;
char cur = 'B';
int tar = n;
for(int i=2*n; i>=1; i--) {
st.push(cur);
// find tar!!
if(cur == 'A') tar--;
if(la[i-1] <= tar && tar <= ra[i-1] && a[i-1] <= (cur == 'A' ? a[i] : b[i])) cur = 'A';
else cur = 'B';
}
while(st.size()) {
cout << st.top();
st.pop();
}
cout << '\n';
}
else {
cout << "-1\n";
}
}
int32_t main(){
precomp();
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |