# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
560981 | Mazaalai | Building 4 (JOI20_building4) | C++17 | 4 ms | 340 KiB |
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;
int n;
const int N = 5e5 + 5;
const int M = 2 * N;
int a[M], b[M], ans[M]; // 0 ? A : B
const int INF = 1.07e9;
signed main() {
// freopen("0.in", "r", stdin);
// freopen("0.out", "w", stdout);
cin >> n;
for (int i = 1; i <= 2 * n; i++) cin >> a[i];
for (int i = 1; i <= 2 * n; i++) cin >> b[i];
a[0] = INF-1; a[2*n+1] = INF-1;
b[0] = 0; b[2*n+1] = 0;
for (int i = 1; i <= 2 * n; i++) {
if (a[i] < a[i-1] && a[i] < b[i-1]) a[i] = INF;
if (b[i] < a[i-1] && b[i] < b[i-1]) b[i] = INF;
}
// for (int i = 1; i <= 2 * n; i++) cout << a[i] << " \n"[i==2*n];
// for (int i = 1; i <= 2 * n; i++) cout << b[i] << " \n"[i==2*n];
int A = 0, B = 0, tar = n+1;
for (int i = 2*n+1; i > 0; i--) {
if ((ans[i] == 0 && a[i] == INF) || (ans[i] == 1 && b[i] == INF)) {
// cout << i << ' ' << ans[i] << '\n';
cout << "-1\n";
return 0;
}
if (ans[i] == 0) {
if (a[i] < min(a[i-1], b[i-1])) {
cout << "-1\n";
return 0;
}
// cout << "HERE: " << a[i-1] << ' ' << a[i] << '\n';
if (a[i-1] > a[i]) ans[i-1] = 1;
} else {
if (b[i] < min(a[i-1], b[i-1])) {
cout << "-1\n";
return 0;
}
if (a[i-1] > b[i]) ans[i-1] = 0;
}
A += ans[i] == 0;
B += ans[i] == 1;
}
A += ans[0] == 0;
B += ans[0] == 1;
int i = 2*n;
while (A > B && i > 1) {
i--;
int x = (ans[i-1] ? b[i-1] : a[i-1]);
int y = (ans[i] ? b[i] : a[i]);
int z = (ans[i+1] ? b[i+1] : a[i+1]);
if (ans[i] == 1) continue;
if (x <= b[i] && b[i] <= z) {
ans[i] = 1;
A--;
B++;
}
}
if (A != B) {
cout <<"-1\n";
return 0;
}
for (int i = 1; i <= 2*n; i++) cout << (ans[i] ? 'B' : 'A'); cout <<'\n';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |