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>
#define PRINT(x) cerr<<#x<<'='<<x<<endl;
#define NL(x) " \n"[(x)]
#define lld long long
#define pil pair<int,lld>
#define pli pair<lld,int>
#define pll pair<lld,lld>
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define mid (l+r)/2
#define endl '\n'
#define all(a) begin(a),end(a)
#define sz(a) int((a).size())
#define LINF 1000000000000000LL
#define INF 1000000000
#define EPS 1e-9
using namespace std;
#define MAXN 500010
int A[2*MAXN], B[2*MAXN];
string rez;
pii Ai[2*MAXN], Bi[2*MAXN];
pii unija(pii A, pii B) {
return {min(A.fi, B.fi), max(A.se, B.se)};
}
bool in(int x, pii A) {
return (A.fi <= x && x <= A.se);
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0);
int N; cin >> N;
for(int i = 1; i <= 2*N; i++) cin >> A[i];
for(int i = 1; i <= 2*N; i++) cin >> B[i];
for(int i = 1; i <= 2*N; i++) Ai[i] = Bi[i] = {INF, -INF};
Ai[2*N] = {1, 1}; Bi[2*N] = {0, 0};
for(int i = 2*N-1; i >= 1; i--) {
if(A[i] <= A[i+1]) Ai[i] = unija(Ai[i], Ai[i+1]);
if(A[i] <= B[i+1]) Ai[i] = unija(Ai[i], Bi[i+1]);
Ai[i] = {Ai[i].fi+1, Ai[i].se+1};
if(B[i] <= A[i+1]) Bi[i] = unija(Bi[i], Ai[i+1]);
if(B[i] <= B[i+1]) Bi[i] = unija(Bi[i], Bi[i+1]);
}
int cnt = N, prev = 0;
for(int i = 1; i <= 2*N; i++) {
if(prev <= A[i] && in(cnt, Ai[i])) {
rez += "A";
prev = A[i];
cnt--;
} else if(prev <= B[i] && in(cnt, Bi[i])) {
rez += "B";
prev = B[i];
} else {
cout << -1 << endl;
return 0;
}
}
cout << rez;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |