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;
typedef vector<ll> vii;
typedef pair<ll,ll> pii;
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(v) v.begin(),v.end()
#define pow poww
const ll M=2010;
const ll inf=2e18;
const ll mod=1e9+7;
ll pow(ll x,ll y){
ll res=1;
x%=mod;
while(y>0){
if((y&1))
res*=x,res%=mod;
x*=x,x%=mod;
y=(y>>1);
}
return res;
}
ll n,a[M],b[M];
bool dp[M][M][2];
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
int TC=1;
//cin>>TC;
while(TC--){
cin>>n;
n*=2;
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n;i++)
cin>>b[i];
dp[0][0][0]=dp[0][1][1]=1;
for(int i=1;i<n;i++){
if(a[i]>=b[i-1]){
for(int j=0;j<=i;j++){
if(dp[i-1][j][0])
dp[i][j+1][1]=1;
}
}
if(a[i]>=a[i-1]){
for(int j=0;j<=i;j++){
if(dp[i-1][j][1])
dp[i][j+1][1]=1;
}
}
if(b[i]>=b[i-1]){
for(int j=0;j<=i;j++){
if(dp[i-1][j][0])
dp[i][j][0]=1;
}
}
if(b[i]>=a[i-1]){
for(int j=0;j<=i;j++){
if(dp[i-1][j][1])
dp[i][j][0]=1;
}
}
}
ll cnt=n-1,cur=n/2;
string ans;
if(!dp[cnt][cur][0]&&!dp[cnt][cur][1]){
cout<<-1;
return 0;
}
bool c=0;
if(!dp[cnt][cur][0])
c=1;
if(c)
ans.pb('A'),cur--;
else
ans.pb('B');
cnt--;
while(cnt>0){
if(c){
if(a[cnt+1]>=b[cnt]&&dp[cnt][cur][0]){
ans.pb('B'),c=0;
cnt--;
}
else{
ans.pb('A'),c=1;
cnt--,cur--;
}
}
else{
if(b[cnt+1]>=b[cnt]&&dp[cnt][cur][0]){
ans.pb('B'),c=0;
cnt--;
}
else{
ans.pb('A'),c=1;
cnt--,cur--;
}
}
}
if(cur)
ans.pb('A');
else
ans.pb('B');
reverse(all(ans));
cout<<ans;
}
return 0;
}
/*
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |