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;
using i64 = long long;
using pii = pair<int, int>;
const int MAXN = 5e5 + 5;
#define prev ___prev
const int oo32 = 1e9 + 5;
int N;
int a[MAXN];
int b[MAXN];
int ans[MAXN];
template <class T1, class T2>
bool maximize(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
}
return false;
}
namespace subtask {
const int MAXN = 4e3 + 5;
bool dp[MAXN][MAXN][2];
array<int, 3> prev[MAXN][MAXN][2];
void solution() {
dp[0][0][0] = dp[0][0][1] = true;
for (int i = 1; i <= 2 * N; i++) {
if (a[i - 1] <= a[i]) {
for (int j = 0; j <= N; j++) {
if (dp[i - 1][j][0]) {
dp[i][j][0] = true;
prev[i][j][0] = {i - 1, j, 0};
}
}
}
if (b[i - 1] <= a[i]) {
for (int j = 0; j <= N; j++) {
if (dp[i - 1][j][1]) {
dp[i][j][0] = true;
prev[i][j][0] = {i - 1, j, 1};
}
}
}
if (a[i - 1] <= b[i]) {
for (int j = 1; j <= N; j++) {
if (dp[i - 1][j - 1][0]) {
dp[i][j][1] = true;
prev[i][j][1] = {i - 1, j - 1, 0};
}
}
}
if (b[i - 1] <= b[i]) {
for (int j = 1; j <= N; j++) {
if (dp[i - 1][j - 1][1]) {
dp[i][j][1] = true;
prev[i][j][1] = {i - 1, j - 1, 1};
}
}
}
}
if (dp[2 * N][N][0]) {
array<int, 3> cur = {2 * N, N, 0};
while (cur[0]) {
ans[cur[0]] = cur[2];
cur = prev[cur[0]][cur[1]][cur[2]];
}
for (int i = 1; i <= 2 * N; i++)
cout << (ans[i] ? 'B' : 'A');
} else if (dp[2 * N][N][1]) {
array<int, 3> cur = {2 * N, N, 1};
while (cur[0]) {
ans[cur[0]] = cur[2];
cur = prev[cur[0]][cur[1]][cur[2]];
}
for (int i = 1; i <= 2 * N; i++)
cout << (ans[i] ? 'B' : 'A');
} else
cout << -1;
}
}
int dp[MAXN][2][2];
array<int, 3> prev[MAXN][2][2];
void update_minn(array<int, 3> a, array<int, 3> b, int x) {
if (minimize(dp[a[0]][a[1]][a[2]], dp[b[0]][b[1]][b[2]] + x))
prev[a[0]][a[1]][a[2]] = b;
}
void update_maxx(array<int, 3> a, array<int, 3> b, int x) {
if (maximize(dp[a[0]][a[1]][a[2]], dp[b[0]][b[1]][b[2]] + x))
prev[a[0]][a[1]][a[2]] = b;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 1; i <= 2 * N; i++)
cin >> a[i];
for (int i = 1; i <= 2 * N; i++)
cin >> b[i];
for (int i = 1; i <= 2 * N; i++) {
dp[i][0][0] = dp[i][1][0] = oo32;
dp[i][0][1] = dp[i][1][1] = -oo32;
}
for (int i = 1; i <= 2 * N; i++) {
if (a[i - 1] <= a[i]) {
update_minn({i, 0, 0}, {i - 1, 0, 0}, 1);
update_maxx({i, 0, 1}, {i - 1, 0, 1}, 1);
}
if (b[i - 1] <= a[i]) {
update_minn({i, 0, 0}, {i - 1, 1, 0}, 1);
update_maxx({i, 0, 1}, {i - 1, 1, 1}, 1);
}
if (a[i - 1] <= b[i]) {
update_minn({i, 1, 0}, {i - 1, 0, 0}, 0);
update_maxx({i, 1, 1}, {i - 1, 0, 1}, 0);
}
if (b[i - 1] <= b[i]) {
update_minn({i, 1, 0}, {i - 1, 1, 0}, 0);
update_maxx({i, 1, 1}, {i - 1, 1, 1}, 0);
}
}
// for(int i = 1; i <= 2 * N; i++) {
// printf("%d %d %d %d %d\n", i, dp[i][0][0], dp[i][0][1], dp[i][1][0], dp[i][1][1]);
// }
if(dp[2 * N][0][0] <= N and N <= dp[2 * N][0][1]) {
array<int, 3> cur = {2 * N, 0, 1};
int cnt = 0;
for(int i = 2 * N; i >= 1 and cnt < N; i--) {
ans[cur[0]] = (cur[1] ^ 1);
if(ans[cur[0]])
cnt++;
cur = prev[cur[0]][cur[1]][cur[2]];
}
} else if(dp[2 * N][1][0] <= N and N <= dp[2 * N][1][1]) {
array<int, 3> cur = {2 * N, 1, 1};
int cnt = 0;
for(int i = 2 * N; i >= 1 and cnt < N; i--) {
ans[cur[0]] = (cur[1] ^ 1);
if(ans[cur[0]])
cnt++;
cur = prev[cur[0]][cur[1]][cur[2]];
}
} else return cout << -1, 0;
for(int i = 1; i <= 2 * N; i++)
cout << (ans[i] ? 'A' : 'B');
return (0 ^ 0);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |