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>
using namespace std;
const int N = 5e5 + 5;
int a[2 * N], b[2 * N];
struct ura{
int l, r;
};
ura dp[N][2];
void join(ura &x, ura y)
{
x.l = min(x.l, y.l);
x.r = max(x.r, y.r);
}
char s[2 * N];
int cnt[2];
int main()
{
int n, i, ans = -1, j;
cin >> n;
cnt[0] = cnt[1] = n;
for (i = 1; i <= 2 * n; i++)
for (j = 0; j < 2; j++)
dp[i][j].l = 2 * N;
for (i = 1; i <= 2 * n; i++)
cin >> a[i];
for (i = 1; i <= 2 * n; i++)
cin >> b[i];
dp[1][0] = {1, 1};
dp[1][1] = {0, 0};
for (i = 1; i < 2 * n; i++)
{
if (a[i] <= a[i + 1])
join(dp[i + 1][0], {dp[i][0].l + 1, dp[i][0].r + 1});
if (a[i] <= b[i + 1])
join(dp[i + 1][1], dp[i][0]);
if (b[i] <= b[i + 1])
join(dp[i + 1][1], dp[i][1]);
if (b[i] <= a[i + 1])
join(dp[i + 1][0], {dp[i][1].l + 1, dp[i][1].r + 1});
}
for (i = 0; i < 2; i++)
if (dp[2 * n][i].l <= n && n <= dp[2 * n][i].r)
ans = i;
if (ans == -1)
{
cout << ans;
return 0;
}
s[2 * n] = ans + 'A';
cnt[ans]--;
int lst = b[2 * n];
if (ans == 0)
lst = a[2 * n];
for (i = 2 * n - 1; 1 <= i; i--)
{
if (dp[i][0].l <= cnt[0] && cnt[0] <= dp[i][0].r && a[i] <= lst)
{
lst = a[i];
cnt[0]--;
s[i] = 'A';
}
else
{
lst = b[i];
cnt[1]--;
s[i] = 'B';
}
}
cout << s + 1;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |