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;
typedef long long ll;
const int N = 1e6+56;
int n,a[N],b[N],c[N];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1; i <= 2*n; i++){
cin >> a[i];
}
for(int i = 1; i <= 2*n; i++){
cin >> b[i];
}
string s = "";
int cnta = 0;
for(int i = 1; i <= 2*n; i++){
if(a[i] < b[i]){
if(a[i] >= c[i-1]){
s += "A";
cnta++;
c[i] = a[i];
}
else{
if(b[i] < c[i-1]){
cout << -1 << endl;
return 0;
}
s += "B";
c[i] = b[i];
}
}
else{
if(b[i] >= c[i-1]){
s += "B";
c[i] = b[i];
}
else{
if(a[i] < c[i-1]){
cout << -1 << endl;
return 0;
}
s += "A";
cnta++;
c[i] = a[i];
}
}
}
//cout << "pre " << s << " " << cnta << endl;
if(cnta > n){
for(int i = 2*n; i >= 1; i--){
if(cnta == n) break;
if(s[i-1] == 'B') continue;
if((i == 1 || b[i] >= c[i-1]) && (i == 2*n || b[i] <= c[i+1])){
cnta--;
c[i] = b[i];
s[i-1] = 'B';
}
}
if(cnta != n){
cout << -1 << endl;
return 0;
}
}
else if(cnta < n){
for(int i = 2*n; i >= 1; i--){
if(cnta == n) break;
if(s[i-1] == 'A') continue;
if((i == 1 || a[i] >= c[i-1]) && (i == 2*n || a[i] <= c[i+1])){
cnta++;
c[i] = a[i];
s[i-1] = 'A';
}
}
if(cnta != n){
cout << -1 << endl;
return 0;
}
}
cout << s << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |