이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for(ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
#define f first
#define s second
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define pb push_back
#define all(v) v.begin(), v.end()
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
const ll INF = 1e18 + 100;
const pi PINF = pi(INF, INF);
ll n, a[1000005], b[1000005], v, rv;
pi range[1000005][2]; //0 A, 1 B
string ans;
bool A;
inline pi add(pi a, ll v) {
return pi(a.f+v, a.s+v);
}
inline pi merge(pi a, pi b) {
if (a == PINF) return b;
if (b == PINF) return a;
return pi(min(a.f, b.f), max(a.s, b.s));
}
inline bool inter(pi a, int b) {
return (a.f <= b and b <= a.s);
}
void fail() {
cout << -1;
exit(0);
}
int main() {
fastio; cin >> n;
FOR(i, 1, 2*n) cin >> a[i];
FOR(i, 1, 2*n) cin >> b[i];
range[1][0] = pi(1, 1);
range[1][1] = pi(-1, -1);
FOR(i, 2, 2*n) {
range[i][0] = PINF; range[i][1] = PINF;
if (a[i] >= a[i-1] and range[i-1][0] != PINF) range[i][0] = add(range[i-1][0], 1);
if (a[i] >= b[i-1] and range[i-1][1] != PINF) range[i][0] = merge(range[i][0], add(range[i-1][1], 1));
if (b[i] >= a[i-1] and range[i-1][0] != PINF) range[i][1] = add(range[i-1][0], -1);
if (b[i] >= b[i-1] and range[i-1][1] != PINF) range[i][1] = merge(range[i][1], add(range[i-1][1], -1));
}
v = -1, A = 0, rv = INF;
DEC(i, 2*n+1, 2) {
v = v + (A ? -1 : 1);
if (inter(range[i-1][0], v) and rv >= a[i-1]) A = 1;
else if (inter(range[i-1][1], v) and rv >= b[i-1]) A = 0;
else fail();
ans += (A ? 'A' : 'B');
rv = (A ? a[i-1] : b[i-1]);
}
reverse(all(ans));
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |