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;
int a[1000005], b[1000005];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, l=0, r=0, k=0, cnt=0;
cin >> n;
for (int i=1; i<=n*2; i++)
cin >> a[i];
for (int i=1; i<=n*2; i++)
cin >> b[i];
for (int i=2; i<=n*2; i++)
{
int mn=min(a[i-1]==-1?2e9:a[i-1], b[i-1]==-1?2e9:b[i-1]);
if (a[i]<mn)
a[i]=-1;
if (b[i]<mn)
b[i]=-1;
}
for (int i=n*2-1; i>=1; i--)
{
int mx=max(a[i+1], b[i+1]);
if (a[i]>mx)
a[i]=-1;
if (b[i]>mx)
b[i]=-1;
}
for (int i=1; i<=n*2; i++)
{
if (a[i]==-1 && b[i]==-1)
{
cout << -1;
return 0;
}
else if (b[i]==-1)
{
l++;
r++;
k++;
}
else if (a[i]!=-1)
{
int x=0, y=0, mn=0, mx=0;
while (i<n*2 && a[i+1]!=-1 && b[i+1]!=-1 && max(a[i], b[i])>min(a[i+1], b[i+1]))
{
if (a[i]>b[i])
{
x++;
y--;
}
else
y++;
mn=min(mn, y);
mx=max(mx, y);
i++;
}
if (a[i]>b[i])
{
x++;
y--;
}
else
y++;
l+=x+min(mn, y);
r+=x+max(mx, y);
k+=x;
}
}
if (n<l || r<n)
{
cout << -1;
return 0;
} /*
for (int i=1; i<=n*2; i++)
cout << a[i] << ' ';
cout << '\n';
for (int i=1; i<=n*2; i++)
cout << b[i] << ' ';
cout << '\n';
cout << "l r " << l << ' ' << r << '\n'; */
for (int i=1; i<=n*2; i++)
{
if (a[i]==-1)
cout << 'B';
else if (b[i]==-1)
cout << 'A';
else
{
int ind=i, y=0, mn=0, mx=0, tar;
while (ind<n*2 && a[ind+1]!=-1 && b[ind+1]!=-1 && max(a[ind], b[ind])>min(a[ind+1], b[ind+1]))
{
if (a[ind]>b[ind])
y--;
else
y++;
mn=min(mn, y);
mx=max(mx, y);
ind++;
}
if (a[ind]>b[ind])
y--;
else
y++;
mn=min(mn, y);
mx=max(mx, y);
if (k+cnt<=n)
tar=min(n-k-cnt, mx);
else
tar=max(n-k-cnt, mn);
cnt+=tar;
y=0;
while (i<n*2 && a[i+1]!=-1 && b[i+1]!=-1 && max(a[i], b[i])>min(a[i+1], b[i+1]))
{
if (y==tar)
{
if (a[i]>b[i])
cout << 'A';
else
cout << 'B';
}
else
{
if (a[i]>b[i])
{
cout << 'B';
y--;
}
else
{
cout << 'A';
y++;
}
}
i++;
}
if (y==tar)
{
if (a[i]>b[i])
cout << 'A';
else
cout << 'B';
}
else
{
if (a[i]>b[i])
cout << 'B';
else
cout << 'A';
}
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |