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;
const int N = 1000000;
int height[N][2], low[N][2], high[N][2];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2 * n; ++j) {
cin >> height[j][i];
}
}
low[0][0] = high[0][0] = 0;
low[0][1] = high[0][1] = 1;
for (int i = 1; i < 2 * n; ++i) {
for (int j = 0; j < 2; ++j) {
low[i][j] = N, high[i][j] = -N;
for (int k = 0; k < 2; ++k) {
if (low[i - 1][k] <= high[i - 1][k] && height[i][j] >= height[i - 1][k]) {
low[i][j] = min(low[i][j], low[i - 1][k] + j);
high[i][j] = max(high[i][j], high[i - 1][k] + j);
}
}
}
}
int i = 2 * n - 1, j;
if (low[2 * n - 1][0] <= n && n <= high[2 * n - 1][0]) {
j = 0;
} else if (low[2 * n - 1][1] <= n && n <= high[2 * n - 1][1]) {
j = 1;
} else {
cout << -1 << "\n";
exit(0);
}
string ans;
while (i >= 0) {
ans.push_back(j == 0 ? 'A' : 'B');
n -= j, --i;
j = low[i][1] <= n && n <= high[i][1] && height[i][1] <= height[i + 1][j];
}
reverse(ans.begin(), ans.end());
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |