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;
struct date
{
int l,r;
};
int n,a[1000005],b[1000005];
int last;
char sol[1000005];
date dp[2][1000005]; /// 0 -> A, 1 -> B
date plsput(date a, date b)
{
if(a.l==-1)
return b;
a.l=min(a.l,b.l);
a.r=max(a.r,b.r);
return a;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
n*=2;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>b[i];
dp[0][1]={1,1};
dp[1][1]={0,0};
for(int i=2;i<=n;i++)
{
dp[0][i]={-1,-1};
dp[1][i]={-1,-1};
if(dp[0][i-1].l!=-1&&a[i]>=a[i-1])
dp[0][i]=plsput(dp[0][i],{dp[0][i-1].l+1,dp[0][i-1].r+1});
if(dp[1][i-1].l!=-1&&a[i]>=b[i-1])
dp[0][i]=plsput(dp[0][i],{dp[1][i-1].l+1,dp[1][i-1].r+1});
if(dp[0][i-1].l!=-1&&b[i]>=a[i-1])
dp[1][i]=plsput(dp[1][i],{dp[0][i-1].l,dp[0][i-1].r});
if(dp[1][i-1].l!=-1&&b[i]>=b[i-1])
dp[1][i]=plsput(dp[1][i],{dp[1][i-1].l,dp[1][i-1].r});
}
int poz=n;
int need=n/2;
int last=2e9;
while(poz>0)
{
if(dp[0][poz].l<=need&&dp[0][poz].r>=need&&a[poz]<=last)
{
last=a[poz];
sol[poz]='A';
need--;
poz--;
continue;
}
if(dp[1][poz].l<=need&&dp[1][poz].r>=need&&b[poz]<=last)
{
last=b[poz];
sol[poz]='B';
poz--;
continue;
}
cout<<-1;
return 0;
}
for(int i=1;i<=n;i++)
cout<<sol[i];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |