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>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
const int INF = 0x3f3f3f3f;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector< vector<int> > a(2, vector<int>(2 * n + 1));
for (int i = 0; i < 2; i++) {
for (int j = 1; j <= 2 * n; j++) {
cin >> a[i][j];
}
}
vector< vector<int> > dp_r(2 * n + 1, vector<int>(2, -1));
vector< vector<int> > dp_l(2 * n + 1, vector<int>(2, INF));
dp_r[0][0] = dp_r[0][1] = dp_l[0][0] = dp_l[0][1] = 0;
for (int i = 0; i < 2 * n; i++) {
for (int j = 0; j < 2; j++) {
if (dp_r[i][j] == -1) {
continue;
}
if (a[j][i] <= a[0][i + 1]) {
dp_r[i + 1][0] = max(dp_r[i + 1][0], dp_r[i][j] + 1);
dp_l[i + 1][0] = min(dp_l[i + 1][0], dp_l[i][j] + 1);
}
if (a[j][i] <= a[1][i + 1]) {
dp_r[i + 1][1] = max(dp_r[i + 1][1], dp_r[i][j]);
dp_l[i + 1][1] = min(dp_l[i + 1][1], dp_l[i][j]);
}
}
}
int cur;
if (dp_r[2 * n][0] >= n && dp_l[2 * n][0] <= n) {
cur = 0;
} else if (dp_r[2 * n][1] >= n && dp_l[2 * n][1] <= n) {
cur = 1;
} else {
cout << -1 << '\n';
return 0;
}
string ans = "";
for (int i = 2 * n, cnt = n; i >= 1; i--) {
if (cur == 0) {
ans += 'A';
cnt--;
} else {
ans += 'B';
}
if (a[0][i - 1] <= a[cur][i] && dp_r[i - 1][0] >= cnt && dp_l[i - 1][0] <= cnt) {
cur = 0;
} else {
cur = 1;
}
}
reverse(ans.begin(), ans.end());
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |