Submission #1240300

#TimeUsernameProblemLanguageResultExecution timeMemory
1240300antromancapBuilding 4 (JOI20_building4)C++20
0 / 100
4 ms8256 KiB
#include <bits/stdc++.h>

using namespace std;

template <class T> bool mini(T &x, const T &y) { return y < x ? x = y, 1 : 0; }
template <class T> bool maxi(T &x, const T &y) { return y > x ? x = y, 1 : 0; }

const int N = 1e6 + 1;
int n, a[N][2], mn[N][2], mx[N][2];
char ans[N];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	for (int j = 0; j < 2; j++)
		for (int i = 1; i <= 2 * n; i++) cin >> a[i][j];

	memset(mn, 0x3f, sizeof mn);
	mn[0][0] = mn[0][1] = 0;
	for (int i = 1; i <= 2 * n; i++)
		for (int j = 0; j < 2; j++) {
			for (int k = 0; k < 2; k++) {
				if (a[i][j] < a[i - 1][k]) continue;
				mini(mn[i][j], mn[i - 1][k] + k);
				maxi(mx[i][j], mx[i - 1][k] + k);
			}
		}

	for (int j = 0; j < 2; j++) {
		if (mn[2 * n][j] > n || mx[2 * n][j] < n) continue;
		int t = j, cnt = n, pre = 0;
		for (int i = 2 * n; i; i--) {
			if (i < 2 * n && a[i][t] > a[i + 1][pre]) t ^= 1;
			else if (mn[i][t] > cnt || mx[i][t] < cnt) t ^= 1;
			cnt -= t;
			pre = t;
			ans[i] = t ? 'B' : 'A';
		}
		for (int i = 1; i <= 2 * n; i++) cout << ans[i];
		exit(0);
	}
	cout << -1;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...