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;
using ll = long long;
const int N = 2e3 + 10;
bool dp[2 * N][N][2] = {};
int a[2 * N][2];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n;
cin >> n;
for(int i = 1; i <= 2 * n; i++)
cin >> a[i][0];
for(int i = 1; i <= 2 * n; i++)
cin >> a[i][1];
// int ch[n + 1] = {};
// bool f[n + 1][2] = {};
// f[1][0] = f[1][1] = 1;
// for(int i = 1; i <= n; i++)
dp[0][0][0] = dp[0][0][1] = 1;
for(int i = 1; i <= 2 * n; i++) {
for(int k = 0; k <= n; k++) {
for(int t1 = 0; t1 <= 1; t1++) {
if(!k && t1) continue;
for(int t2 = 0; t2 <= 1; t2++)
if(a[i - 1][t2] <= a[i][t1]) dp[i][k][t1] |= dp[i - 1][k - t1][t2];
}
}
}
if(!(dp[2 * n][n][0] || dp[2 * n][n][1])) {
cout << -1;
return 0;
}
int k = n, it = (dp[2 * n][n][1]);
string s;
for(int i = 2 * n; i >= 1; i--) {
s += (it ? 'B' : 'A');
for(int t = 0; t <= 1; t++) {
if(dp[i - 1][k - it][t] && a[i - 1][t] <= a[i][it]) {
k -= it;
it = t;
break;
}
}
}
reverse(s.begin(), s.end());
for(char c : s)
cout << c;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |