이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
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;
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... |