#include <bits/stdc++.h>
//#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define all(x) x.begin(), x.end()
#define sz(s) (int)s.size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define s second
#define f first
using namespace std;
typedef pair < long long, long long > pll;
typedef pair < int, int > pii;
typedef unsigned long long ull;
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;
typedef long long ll;
typedef double db;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 333;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const int inf = 1e9, maxn = 4e5 + 148, mod = 1e9 + 7, N = 1e6 + 11;
const db eps = 1e-12, pi = 3.14159265359;
const ll INF = 1e18;
int n, a[N], b[N], t[N << 2][2], f[N], l[N], r[N], res[N];
void update (int pos, int lvl, int x, int v = 1, int tl = 1, int tr = n + n) {
if (tl == tr) {
t[v][lvl] = x;
return;
}
int tm = (tl + tr) >> 1;
if (pos <= tm)
update(pos, lvl, x, v << 1, tl, tm);
else
update(pos, lvl, x, v << 1 | 1, tm + 1, tr);
if (!lvl)
t[v][lvl] = min(t[v << 1][lvl], t[v << 1 | 1][lvl]);
else
t[v][lvl] = max(t[v << 1][lvl], t[v << 1 | 1][lvl]);
}
int get (int l, int r, int lvl, int v = 1, int tl = 1, int tr = n + n) {
if (l > r || tl > r || l > tr)
return !lvl ? inf : -inf;
if (l <= tl && tr <= r)
return t[v][lvl];
int tm = (tl + tr) >> 1;
if (!lvl)
return min(get(l, r, lvl, v << 1, tl, tm), get(l, r, lvl, v << 1 | 1, tm + 1, tr));
return max(get(l, r, lvl, v << 1, tl, tm), get(l, r, lvl, v << 1 | 1, tm + 1, tr));
}
main () {
scanf("%d", &n);
for (int i = 1; i <= n + n; ++i)
scanf("%d", &a[i]);
for (int i = 1; i <= n + n; ++i)
scanf("%d", &b[i]);
for (int i = 1; i <= n + n;) {
f[i] = i;
int j = i + 1;
while (j <= n + n && a[j] >= a[j - 1]) {
f[j] = i;
++j;
}
i = j;
}
for (int i = 1; i <= n + n; ++i) {
l[i] = inf, r[i] = -inf;
if (i == 1 || (f[i - 1] == 1 && b[i] >= a[i - 1])) //case when i is the 1st element
l[i] = r[i] = 0;
if (i > 1 && b[i] >= b[i - 1]) {
l[i] = min(l[i], l[i - 1]);
r[i] = max(r[i], r[i - 1]);
}
if (i > 1 && b[i] >= a[i - 1]) {
l[i] = min(l[i], get(max(1, f[i - 1] - 1), i - 2, 0));
r[i] = max(r[i], get(max(1, f[i - 1] - 1), i - 2, 1));
}
if (l[i] < inf) {
l[i]++;
r[i]++;
}
if (b[i] <= a[i + 1]) {
update(i, 0, l[i]);
update(i, 1, r[i]);
} else {
update(i, 0, inf);
update(i, 1, -inf);
}
}
int lst = -1;
for (int i = n + n; i > 0; --i) {
if (l[i] <= n && n <= r[i]) {
lst = i;
break;
}
if (a[i] < a[i - 1])
break;
}
if (lst == -1) {
cout << lst << endl;
return 0;
}
int cur = n;
res[lst] = 1;
int timer = 0;
while (--cur) {
++timer;
assert(lst > 1);
if (b[lst - 1] <= b[lst] && l[lst - 1] <= cur && cur <= r[lst - 1]) {
res[--lst] = 1;
continue;
}
assert(a[lst - 1] <= b[lst]);
if (lst == 3) {
}
while (--lst) {
if (b[lst - 1] <= a[lst] && l[lst - 1] <= cur && cur <= r[lst - 1])
break;
}
res[--lst] = 1;
}
for (int i = 1; i <= n + n; ++i)
printf("%c", 'A' + res[i]);
}
Compilation message
building4.cpp:74:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
building4.cpp: In function 'int main()':
building4.cpp:75:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
building4.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
building4.cpp:81:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &b[i]);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
7 ms |
512 KB |
Output is correct |
6 |
Correct |
8 ms |
628 KB |
Output is correct |
7 |
Correct |
7 ms |
512 KB |
Output is correct |
8 |
Correct |
8 ms |
640 KB |
Output is correct |
9 |
Incorrect |
7 ms |
640 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
7 ms |
512 KB |
Output is correct |
6 |
Correct |
8 ms |
628 KB |
Output is correct |
7 |
Correct |
7 ms |
512 KB |
Output is correct |
8 |
Correct |
8 ms |
640 KB |
Output is correct |
9 |
Incorrect |
7 ms |
640 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |