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;
pair<int, int> f[1000005][2];
int a[1000005][2], res[1000005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n * 2; i++) {
cin >> a[i][0];
}
for (int i = 1; i <= n * 2; i++) {
cin >> a[i][1];
f[i][0] = f[i][1] = {-1e9, 1e9};
}
f[1][0] = {0, 0};
f[1][1] = {1, 1};
for (int i = 2; i <= n * 2; i++) {
if (a[i][0] >= a[i - 1][0]) {
f[i][0].first = max(f[i][0].first, f[i - 1][0].first);
f[i][0].second = min(f[i][0].second, f[i - 1][0].second);
}
if (a[i][0] >= a[i - 1][1]) {
f[i][0].first = max(f[i][0].first, f[i - 1][1].first);
f[i][0].second = min(f[i][0].second, f[i - 1][1].second);
}
if (a[i][1] >= a[i - 1][0]) {
f[i][1].first = max(f[i][1].first, f[i - 1][0].first + 1);
f[i][1].second = min(f[i][1].second, f[i - 1][0].second + 1);
}
if (a[i][1] >= a[i - 1][1]) {
f[i][1].first = max(f[i][1].first, f[i - 1][1].first + 1);
f[i][1].second = min(f[i][1].second, f[i - 1][1].second + 1);
}
}
int flag = -1;
if (f[n * 2][0].first >= n && f[n * 2][0].second <= n) flag = 0;
else if (f[n * 2][1].first >= n && f[n * 2][1].second <= n) flag = 1;
if (flag == -1) {
cout << -1;
return 0;
}
int h = n * 2, c = n;
while (h) {
res[h] = flag;
c -= flag;
for (int j = 0; j < 2; j++) {
if (a[h - 1][j] <= a[h][flag]) {
if (f[h - 1][j].first >= c && f[h - 1][j].second <= c) {
flag = j;
break;
}
}
}
h--;
}
for (int i = 1; i <= n * 2; i++) {
if (res[i] == 0) cout << 'A';
else cout << 'B';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |