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;
const int N = 4000 + 12, MOD = 998244353;
typedef long long ll;
int n, a[N][2];
bool dp[N][2];
int dp_[N][N][2];
void test() {
cin >> n;
memset(dp_,-1,sizeof(dp_));
for (int i = 1; i <= n + n; i++) {
cin >> a[i][0];
}
for (int i = 1; i <= n + n; i++) {
cin >> a[i][1];
}
dp[n + n][0] = dp[n + n][1] = 1;
dp_[0][0][0] = 0;
for (int i = n + n - 1; i >= 1; i--) {
for (int j = 0; j <= 1; j++) {
for (int k = 0; k <= 1; k++) {
if (a[i][j] <= a[i + 1][k]) {
dp[i][j] |= dp[i + 1][k];
}
}
}
}
if (!dp[1][0] && !dp[1][1]) {
cout << -1;
return;
}
a[n + n + 1][1] = 1e9 + 2;
for(int i = 1;i <= n + n;i++){
for(int j = 0;j <= 1;j++){
for(int k = 0;k <= n;k++){
if(j == 1){
if(a[i][j] >= a[i - 1][0] && dp_[i - 1][k][0] != -1){
dp_[i][k][1] = 0;
}
if(a[i][j] >= a[i - 1][1] && dp_[i - 1][k][1] != -1){
dp_[i][k][1] = 1;
}
}else{
if(k){
if(a[i][j] >= a[i - 1][0] && dp_[i - 1][k - 1][0] != -1){
dp_[i][k][j] = 0;
}
if(a[i][j] >= a[i - 1][1] && dp_[i - 1][k - 1][1] != -1){
dp_[i][k][j] = 1;
}
}
}
}
}
}
if(dp_[n + n][n][1] == -1 && dp_[n + n][n][0] == -1){
cout << -1;
return;
}
vector<char> res;
int val = n,lst = 0;
if(dp_[n + n][n][lst] == -1){
lst = 1;
}
for(int i = n + n;i >= 1;i--){
bool ok = false;
if(lst == 0){
res.push_back('A');
ok = 1;
}else res.push_back('B');
lst = dp_[i][val][lst];
val -= ok;
}
reverse(res.begin(),res.end());
for(auto i:res){
cout << i;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--)
test();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |