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 = 1e6 + 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() {
#define TASK "code"
if (fopen(TASK ".inp", "r")) {
freopen(TASK ".inp", "r", stdin);
freopen(TASK ".out", "w", stdout);
}
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] and (dp[i - 1][0][0] != oo32 and dp[i - 1][0][1] != -oo32)) {
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] and (dp[i - 1][1][0] != oo32 and dp[i - 1][1][1] != -oo32)) {
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] and (dp[i - 1][0][0] != oo32 and dp[i - 1][0][1] != -oo32)) {
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] and (dp[i - 1][1][0] != oo32 and dp[i - 1][1][1] != -oo32)) {
update_minn({i, 1, 0}, {i - 1, 1, 0}, 0);
update_maxx({i, 1, 1}, {i - 1, 1, 1}, 0);
}
}
int OK = -1;
if(dp[2 * N][0][0] <= N and N <= dp[2 * N][0][1])
OK = 1;
else if(dp[2 * N][1][0] <= N and dp[2 * N][1][1])
OK = 0;
else return cout << -1, 0;
ans[2 * N] = OK;
int cnt = N - OK;
for(int i = 2 * N - 1; i >= 1; i--) {
int last = (ans[i + 1] == 0 ? b[i + 1] : a[i + 1]);
if(dp[i][0][0] <= cnt and cnt <= dp[i][0][1] and cnt > 0 and a[i] <= last) {
OK = 1;
cnt--;
} else OK = 0;
ans[i] = OK;
}
for(int i = 1; i <= 2 * N; i++)
cout << (ans[i] ? 'A' : 'B');
return (0 ^ 0);
}
Compilation message (stderr)
building4.cpp: In function 'int main()':
building4.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen(TASK ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
building4.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | freopen(TASK ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |