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 <iostream>
# include <vector>
# include <algorithm>
# include <cassert>
using namespace std;
const int MAX=1e6+11,INF=1e9;
int a[2][MAX];
pair<int, int> dp[2][MAX];
int n;
void reset()
{
for(int i=1;i<=n*2;i++)
{
for(int k=0;k<=1;k++)
{
dp[k][i]={INF,-INF};
}
}
}
vector<char> ve;
void rec(int k, int i, int v)
{
if(k==0) ve.push_back('A');
else ve.push_back('B');
if(i==1) return ;
int pr=v;
if(k==1) pr++;
else pr--;
if(dp[0][i-1].first!=INF and dp[0][i-1].first<=pr and pr<=dp[0][i-1].second and a[k][i]>=a[0][i-1]) rec(0,i-1,pr);
else rec(1,i-1,pr);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n;
for(int k=0;k<=1;k++)
{
for(int i=1;i<=n*2;i++) cin>>a[k][i];
}
reset();
dp[0][1]={1,1};
dp[1][1]={-1,-1};
for(int i=1;i<n*2;i++)
{
for(int k=0;k<=1;k++)
{
if(dp[k][i].first==INF) continue;
if(a[k][i]<=a[0][i+1])
{
int l=dp[k][i].first+1;
int r=dp[k][i].second+1;
dp[0][i+1].first=min(dp[0][i+1].first,l);
dp[0][i+1].second=max(dp[0][i+1].second,r);
}
if(a[k][i]<=a[1][i+1])
{
int l=dp[k][i].first-1;
int r=dp[k][i].second-1;
dp[1][i+1].first=min(dp[1][i+1].first,l);
dp[1][i+1].second=max(dp[1][i+1].second,r);
}
}
}
/*
for(int i=1;i<=n*2;i++)
{
for(int k=0;k<=1;k++)
{
cout<<i<<" "<<k<<":"<<dp[k][i].first<<" "<<dp[k][i].second<<"\n";
}
}
*/
if(!(dp[0][n*2].first<=0 and dp[0][n*2].second>=0) and !(dp[1][n*2].first<=0 and dp[1][n*2].second>=0)) {cout<<"-1"<<"\n";return 0;}
if(dp[0][n*2].first<=0 and dp[0][n*2].second>=0) rec(0,n*2,0);
else rec(1,n*2,0);
reverse(ve.begin(),ve.end());
for(char x: ve) cout<<x;
cout<<"\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |