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 ll long long
#define MAXN 1000010
using namespace std;
ll a[2][MAXN],dp[2][MAXN][2];
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n;
cin >> n;
for (ll i=0;i<2;i++)
{
for (ll j=1;j<=2*n;j++)
cin >> a[i][j];
}
dp[0][1][0]=dp[0][1][1]=1;
dp[1][1][0]=dp[1][1][1]=0;
for (ll i=2;i<=2*n;i++)
{
dp[0][i][0]=dp[1][i][0]=LLONG_MAX;
dp[0][i][1]=dp[1][i][1]=LLONG_MIN;
for (ll j=0;j<2;j++)
{
if (a[0][i]>=a[j][i-1])
{
dp[0][i][0]=min(dp[0][i][0],dp[j][i-1][0]+1);
dp[0][i][1]=max(dp[0][i][1],dp[j][i-1][1]+1);
}
if (a[1][i]>=a[j][i-1])
{
dp[1][i][0]=min(dp[1][i][0],dp[j][i-1][0]);
dp[1][i][1]=max(dp[1][i][1],dp[j][i-1][1]);
}
}
}
if ((dp[0][2*n][0]>n || dp[0][2*n][1]<n) && (dp[1][2*n][0]>n || dp[1][2*n][1]<n))
{
cout << -1;
return 0;
}
ll val=n,pos=2*n,i=dp[0][2*n][0]<=n || dp[0][2*n][1]>=n?0 : 1;
string ans=i?"B" : "A";
while (pos>1)
{
val-=1-i;
i=dp[0][pos-1][0]<=val && dp[0][pos-1][1]>=val && a[0][pos-1]<=a[i][pos]?0 : 1;
ans=(i?"B" : "A")+ans;
pos--;
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |