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 int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N=5e5+5;
pair <int,int> dp[N*2][2];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n;
cin>>n;
vector <int> a(n*2+1),b(n*2+1);
for(int i=1;i<=n*2;i++){
cin>>a[i];
}
for(int i=1;i<=n*2;i++){
cin>>b[i];
}
dp[1][0]={0,0};
dp[1][1]={1,1};
for(int i=2;i<=n*2;i++){
dp[i][0]={1e9,-1e9};
dp[i][1]={1e9,-1e9};
}
for(int i=2;i<=n*2;i++){
if(a[i]>=a[i-1]){
dp[i][0].ff=min(dp[i][0].ff,dp[i-1][0].ff);
dp[i][0].ss=max(dp[i][0].ss,dp[i-1][0].ss);
}
if(a[i]>=b[i-1]){
dp[i][0].ff=min(dp[i][0].ff,dp[i-1][1].ff);
dp[i][0].ss=max(dp[i][0].ss,dp[i-1][1].ss);
}
if(b[i]>=b[i-1]){
dp[i][1].ff=min(dp[i][1].ff,dp[i-1][1].ff+1);
dp[i][1].ss=max(dp[i][1].ss,dp[i-1][1].ss+1);
}
if(b[i]>=a[i-1]){
dp[i][1].ff=min(dp[i][1].ff,dp[i-1][0].ff+1);
dp[i][1].ss=max(dp[i][1].ss,dp[i-1][0].ss+1);
}
}
int pos=n*2,cnt=n,tp=-1;
if(dp[n*2][0].ff<=n && n<=dp[n*2][0].ss)tp=0;
else if(dp[n*2][1].ff<=n && n<=dp[n*2][1].ss)tp=1;
if(tp==-1){
cout<<-1<<"\n";
return 0;
}
string ans="";
while(pos>=1){
if(tp==0){
ans+='A';
if(a[pos]>=a[pos-1] && dp[pos-1][0].ff<=cnt && cnt<=dp[pos-1][0].ss)tp=0;
else tp=1;
}
else{
ans+='B';
cnt--;
if(b[pos]>=a[pos-1] && dp[pos-1][0].ff<=cnt && cnt<=dp[pos-1][0].ss)tp=0;
else tp=1;
}
pos--;
}
reverse(all(ans));
cout<<ans<<"\n";
}
/*
2 5 4 9 15 11
6 7 6 8 12 14
1 0 0 0
1 0 0 0
0 0 0 0
0 1 0 0
0 1 1 0
0 0 0 0
0 1 0 0
0 1 1 0
0 1 0 0
0 0 1 0
0 0 1 1
0 0 0 1
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |