이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
int a[N][2];
int dp[N][2][2];
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int j = 0; j < 2; ++j) {
for (int i = 0; i < 2 * n; ++i) {
cin >> a[i][j];
}
}
dp[0][0][0] = dp[0][0][1] = 1;
for (int i = 1; i < 2 * n; ++i) {
for (int j = 0; j < 2; ++j) {
dp[i][j][0] = 1e9;
dp[i][j][1] = -1e9;
for (int k = 0; k < 2; ++k) {
if (a[i - 1][k] > a[i][j]) {
continue;
}
dp[i][j][0] = min(dp[i][j][0], dp[i - 1][k][0] + (j ^ 1));
dp[i][j][1] = max(dp[i][j][1], dp[i - 1][k][1] + (j ^ 1));
}
}
}
for (int it = 0; it < 2; ++it) {
if (dp[2 * n - 1][it][0] > n or dp[2 * n - 1][it][1] < n) {
continue;
}
string ans;
ans += char('A' + it);
int tot = n - (it ^ 1);
for (int i = 2 * n - 2; i >= 0; --i) {
for (int j = 0; j < 2; ++j) {
if (a[i][j] <= a[i + 1][it] and dp[i][j][0] <= tot and tot <= dp[i][j][1] and tot >= (j ^ 1)) {
ans += char('A' + j);
tot -= (j ^ 1);
it = j;
break;
}
}
}
reverse(ans.begin(), ans.end());
cout << ans;
return 0;
}
cout << -1;
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |