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 <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
using namespace std;
template<typename T1, typename T2> inline void chkmin(T1 &a, T2 b) {if (a > b) a = b;}
template<typename T1, typename T2> inline void chkmax(T1 &a, T2 b) {if (a < b) a = b;}
#define files(FILENAME) read(FILENAME); write(FILENAME)
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c).size()
#define left left228
#define right right228
#define y1 y1228
#define mp make_pair
#define pb push_back
#define y2 y2228
#define rank rank228
using ll = long long;
using ld = long double;
const string FILENAME = "input";
const int MAXN = 500228;
int n;
int a[2 * MAXN], b[2 * MAXN];
int L[2 * MAXN][2], R[2 * MAXN][2];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//read(FILENAME);
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++) {
L[i][0] = L[i][1] = 1e9;
R[i][0] = R[i][1] = -1e9;
}
L[1][0] = 1;
R[1][0] = 1;
L[1][1] = 0;
R[1][1] = 0;
for (int i = 1; i < 2 * n; i++) {
if (a[i] <= a[i + 1]) {
chkmin(L[i + 1][0], L[i][0] + 1);
chkmax(R[i + 1][0], R[i][0] + 1);
}
if (a[i] <= b[i + 1]) {
chkmin(L[i + 1][1], L[i][0]);
chkmax(R[i + 1][1], R[i][0]);
}
if (b[i] <= a[i + 1]) {
chkmin(L[i + 1][0], L[i][1] + 1);
chkmax(R[i + 1][0], R[i][1] + 1);
}
if (b[i] <= b[i + 1]) {
chkmin(L[i + 1][1], L[i][1]);
chkmax(R[i + 1][1], R[i][1]);
}
}
if (!(L[2 * n][0] <= n && R[2 * n][0] >= n) && !(L[2 * n][1] <= n && R[2 * n][1] >= n)) {
cout << -1 << '\n';
return 0;
}
int i = 2 * n;
int cnt = n;
int last = ((L[2 * n][0] <= n && R[2 * n][0] >= n) ? 0 : 1);
string sol = "";
while (i >= 2) {
if (last == 0) {
sol += 'A';
} else {
sol += 'B';
}
if (last == 0) {
if (a[i - 1] <= a[i] && L[i - 1][0] + 1 <= cnt && cnt <= R[i - 1][0] + 1) {
last = 0;
} else if (b[i - 1] <= a[i] && L[i - 1][1] + 1 <= cnt && cnt <= R[i - 1][1] + 1) {
last = 1;
}
cnt--;
} else {
if (a[i - 1] <= b[i] && L[i - 1][0] <= cnt && cnt <= R[i - 1][0]) {
last = 0;
} else if (b[i - 1] <= b[i] && L[i - 1][1] <= cnt && cnt <= R[i - 1][1]) {
last = 1;
}
}
i--;
}
if (last == 0) {
sol += 'A';
} else {
sol += 'B';
}
reverse(all(sol));
cout << sol << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |