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 pb push_back
#define mp make_pair
#define x first
#define y second
using namespace std;
typedef long long ll;
int mas[5][1000005], ans[1000005], type[1000005];
int main()
{
 	//freopen("input.txt", "r", stdin);
 	//freopen("output.txt", "w", stdout);
 	ios_base::sync_with_stdio(0); cin.tie(0);
 	int n;
 	cin >> n;
 	for (int i = 1; i <= 2 * n; i++) cin >> mas[1][i];
 	for (int i = 1; i <= 2 * n; i++) cin >> mas[2][i];
 	for (int i = 1; i <= 2 * n; i++) ans[i] = min(mas[1][i], mas[2][i]);
 	for (int i = 2; i <= 2 * n; i++)
 	{
 	 	if (ans[i] < ans[i - 1]) ans[i] = max(mas[1][i], mas[2][i]);
 	 	if (ans[i] < ans[i - 1]) cout << "-1\n", exit(0);
 	}
 	int bal = 0;
 	for (int i = 1; i <= 2 * n; i++)
 		if (ans[i] == mas[1][i]) bal++, type[i] = 1;
 		else bal--, type[i] = 2;
 	int uk = 1;
 	while (uk <= 2 * n && bal != 0)
 	{
 	    int cop = uk;
 	    while (cop + 1 <= 2 * n && max(mas[1][cop + 1], mas[2][cop + 1]) >= max(mas[1][cop], mas[2][cop])
 	    						&& min(mas[1][cop + 1], mas[2][cop + 1]) < max(mas[1][cop], mas[2][cop])) cop++;
 	    if (cop == 2 * n || max(mas[1][cop], mas[2][cop]) <= min(mas[1][cop + 1], mas[2][cop + 1]))
 	    {
 	    	int mx = 0, del = 0;
 	    	for (int i = cop; i >= uk; i--)
 	    	{
 	    	 	if (type[i] == 1) del -= 2;
 	    	 	else del += 2;
 	    	 	if (bal > 0) mx = min(mx, del);
 	    	 	else mx = max(mx, del);
 	     	}
 	     	del = 0;
 	     	if (bal > 0 && bal + mx < 0) mx = -bal;
 	     	if (bal < 0 && bal + mx > 0) mx = -bal;
 	     	if (mx != 0)
 	     	{
 	     		for (int i = cop; i >= uk; i--)
 	     		{
 	     	 		if (type[i] == 1) del -= 2;
 	     	 		else del += 2;
 	     	 		type[i] = 3 - type[i];
 	     	 		if (del == mx) break; 
 	     		}
 	     	}
 	     	bal += mx;
 	    }
 		uk = cop + 1;
 	}
 	if (bal != 0) cout << "-1\n", exit(0);
 	for (int i = 1; i <= 2 * n; i++)
 		if (type[i] == 1) cout << 'A';
 		else cout << 'B';
 	cout << '\n';
 	return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |