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 pii pair<int,int>
#define ll long long
using namespace std;
int main()
{
int n;
cin>>n;
int dp1[2*n+1][2]={};
int dp2[2*n+1][2]={};
for(int i=0;i<=2*n;i++)dp1[i][0]=dp1[i][1]=1e9+9;
memset(dp2,-1,sizeof(dp2));
int a[2*n+1],b[2*n+1];
for(int i=1;i<=2*n;i++)cin>>a[i];
for(int i=1;i<=2*n;i++)cin>>b[i];
dp1[1][0]=dp2[1][0]=1;
dp1[1][1]=dp2[1][1]=0;
for(int i=2;i<=2*n;i++)
{
if(a[i-1]<=a[i])
{
dp1[i][0]=dp1[i-1][0]+1;
dp2[i][0]=dp2[i-1][0]+1;
}
if(b[i-1]<=a[i])
{
dp1[i][0]=min(dp1[i][0],dp1[i-1][1]+1);
dp2[i][0]=max(dp2[i][0],dp2[i-1][1]+1);
}
if(a[i-1]<=b[i])
{
dp1[i][1]=dp1[i-1][0];
dp2[i][1]=dp2[i-1][0];
}
if(b[i-1]<=b[i])
{
dp1[i][1]=min(dp1[i][1],dp1[i-1][1]);
dp2[i][1]=max(dp2[i][1],dp2[i-1][1]);
}
}
// cout<<dp2[2*n][0]<<endl;
int cr=2*n;
int nx=0;
int tx=n;
vector<int> r;
if(dp1[cr][0]<=n && dp2[cr][0]>=n)
{
r.push_back(0);
tx--;
if(dp1[cr-1][0]<=tx && dp2[cr-1][0]>=tx && a[cr-1]<=a[cr])nx=0;
else nx=1;
}
else if(dp1[cr][1]<=n && dp2[cr][1]>=n)
{
r.push_back(1);
if(dp1[cr-1][0]<=n && dp2[cr-1][0]>=n && a[cr-1]<=b[cr])nx=0;
else nx=1;
}
else
{
cout<<-1<<endl;
return 0;
}
cr--;
while(cr)
{
r.push_back(nx);
if(nx==0 && tx>0)
{
tx--;
if(dp1[cr-1][0]<=tx && dp2[cr-1][0]>=tx && a[cr-1]<=a[cr])nx=0;
else nx=1;
cr--;
continue;
}
else
{
if(dp1[cr-1][0]<=tx && dp2[cr-1][0]>=tx && a[cr-1]<=b[cr])nx=0;
else nx=1;
cr--;
continue;
}
}
reverse(r.begin(),r.end());
for(int i=0;i<r.size();i++)
{
if(r[i]==0)cout<<"A";
else cout<<"B";
}
return 0;
}
Compilation message (stderr)
building4.cpp: In function 'int main()':
building4.cpp:118:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<r.size();i++)
~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |