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>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int N = 1000000 + 10;
const int MOD = 1000000007;
const int LOG = 20;
const int INF = 1000000010;
const int delta = 11353;
pii dp[N][2];
int n, A[N], B[N], ans[N];
pii merge(pii x, pii y){
return {min(x.F, y.F), max(x.S, y.S)};
}
int32_t main(){
ios::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] = dp[i][1] = {-INF, -INF};
dp[0][0] = {0, 0};
for (int i = 1; i <= 2 * n; i++){
// if (min(A[i - 1], B[i - 1]) > max(A[i], B[i])) return cout << -1, 0;
if (A[i] >= B[i - 1] && A[i] >= A[i - 1]) dp[i][0] = merge(dp[i - 1][0], dp[i - 1][1]);
else if(A[i] >= B[i - 1]) dp[i][0] = dp[i - 1][1];
else if(A[i] >= A[i - 1]) dp[i][0] = dp[i - 1][0];
//if (dp[i][0].F < 0) A[i] = INF;
dp[i][0].F++, dp[i][0].S ++;
if (dp[i][0].F < 0) A[i] = INF;
if (B[i] >= B[i - 1] && B[i] >= A[i - 1]) dp[i][1] = merge(dp[i - 1][0], dp[i - 1][1]);
else if(B[i] >= B[i - 1]) dp[i][1] = dp[i - 1][1];
else if (B[i] >= A[i - 1]) dp[i][1] = dp[i - 1][0];
if (dp[i][1].F < 0) B[i] = INF;
//cout << dp[i][0].F << ' ' << dp[i][0].S << ' ' << dp[i][1].F << ' ' << dp[i][1].S << '\n';
}
if ((dp[2 * n][0].F <= n && dp[2 * n][0].S >= n) || (dp[2 * n][1].F <= n && dp[2 * n][1].S >= n)){
pair<pii, int> Now;
if (dp[2 * n][0].F <= n && dp[2 * n][0].S >= n) Now = {{2 * n, 0}, n};
else Now = {{2 * n, 1}, n};
while (Now.F.F != 0){
int id = Now.F.F, u = Now.S;
if (Now.F.S == 0){
if (A[id - 1] <= A[id]){
if (dp[id - 1][0].F <= (u - 1) && dp[id - 1][0].S >= (u - 1)) Now = {{id - 1, 0}, u - 1};
}
if (Now.F.F == id){
Now = {{id - 1, 1}, u - 1};
}
ans[id] = 0;
}else{
if (A[id - 1] <= B[id]){
if (dp[id - 1][0].F <= u && dp[id - 1][0].S >= u) Now = {{id - 1, 0}, u};
}
if (Now.F.F == id){
Now = {{id - 1, 1}, u};
}
ans[id] = 1;
}
}
for (int i = 1; i <= 2 * n; i++) cout << (ans[i]?"B":"A");
}
else cout << -1;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |