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
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> A(2*n+2);
vector<int> B(2*n+2);
A[2*n+1]=B[2*n+1]=1e9;
vector<bool> chosen(2*n+1);
vector<int> val(2*n+2);val[2*n+1]=1e9;
for(int i=1;i<=2*n;i++)cin>>A[i];
for(int i=1;i<=2*n;i++)cin>>B[i];
int cntB = 0;
for(int i=1;i<=2*n;i++){
if(A[i]<=B[i]){
if(A[i]>=val[i-1])val[i]=A[i];
else if(B[i]>=val[i-1]){
chosen[i]=true;
val[i]=B[i];
cntB++;
} else {
cout << "-1\n";
return 0;
}
} else {
if(B[i]>=val[i-1]){
chosen[i]=true;
val[i]=B[i];
cntB++;
} else if(A[i]>=val[i-1])val[i]=A[i];
else {
cout << "-1\n";
return 0;
}
}
}
int cntA = 2*n-cntB;
int end = -1;
int cost = 0;
if(cntA<cntB){
int target = n-cntA;
for(int i=2*n;i;i--){
if(target==0)break;
if((A[i]<B[i] and chosen[i]) or (A[i]>B[i] and !chosen[i])){end=-1;cost=0;continue;} // Already maximum
if(max(A[i],B[i])>max(A[i+1],B[i+1])){end=-1;cost=0;continue;} // Can't be flipped
if(max(A[i],B[i])<=val[i+1])end=i;
else if(end==-1)continue;
if(chosen[i])cost++;
else cost--;
if(cost==1){
for(int j=i;j<=end;j++){
chosen[j]=!chosen[j];
val[j]=max(A[j],B[j]);
}
target--;
cost = 0;
end = -1;
}
}
if(target!=0){
cout << "-1\n";
return 0;
}
} else if(cntB<cntA){
int target = n-cntB;
for(int i=2*n;i;i--){
if(target==0)break;
if((A[i]<B[i] and chosen[i]) or (A[i]>B[i] and !chosen[i])){end=-1;cost=0;continue;} // Already maximum
if(max(A[i],B[i])>max(A[i+1],B[i+1])){end=-1;cost=0;continue;} // Can't be flipped
if(max(A[i],B[i])<=val[i+1])end=i;
else if(end==-1)continue;
if(chosen[i])cost--;
else cost++;
if(cost==1){
for(int j=i;j<=end;j++){
chosen[j]=!chosen[j];
val[j]=max(A[j],B[j]);
}
target--;
cost = 0;
end = -1;
}
}
if(target!=0){
cout << "-1\n";
return 0;
}
}
for(int i=1;i<=2*n;i++)cout<<(chosen[i]?'B':'A');
cout << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |