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>
using namespace std;
const int MAX=4e3+11;
int a[2][MAX];
bool dp[2][MAX][MAX];
int n;
vector<char> ve;
void rec(int k, int i, int v)
{
//cout<<"->"<<k<<" "<<i<<" "<<v-n<<"\n";
if(k==0) ve.push_back('A');
else ve.push_back('B');
if(i==1) return ;
int d=1;
if(k==1) d=-1;
if(v-d>=0 and v-d<=n*2 and dp[0][i-1][v-d]==1 and a[0][i-1]<=a[k][i]) rec(0,i-1,v-d);
else rec(1,i-1,v-d);
}
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];
}
dp[0][1][n+1]=1;
dp[1][1][n-1]=1;
for(int i=1;i<n*2;i++)
{
for(int k=0;k<=1;k++)
{
for(int v=0;v<=n*2;v++)
{
if(dp[k][i][v]==0) continue;
//cout<<i<<" "<<k<<" "<<v-n<<"\n";
if(a[k][i]<=a[0][i+1] and v+1<=n*2) dp[0][i+1][v+1]=1;
if(a[k][i]<=a[1][i+1] and v-1>=0) dp[1][i+1][v-1]=1;
}
}
}
if(!(dp[0][n*2][n]==1 or dp[1][n*2][n]==1)) {cout<<-1<<"\n";return 0;}
if(dp[0][n*2][n]==1) rec(0,n*2,n);
else rec(1,n*2,n);
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... |