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;
typedef long long ll;
const int N = 2002;
int n,a[2*N],b[2*N];
int dp[2*N][2*N][2];
int main(){
ios_base::sync_with_stdio(false);
cin.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];
}
dp[0][0][0] = dp[0][0][1] = 1;
for(int i = 1; i <= 2*n; i++){
for(int j = 0; j <= i; j++){
if(j > 0 && a[i] >= a[i-1]) dp[i][j][0] |= dp[i-1][j-1][0];
if(j > 0 && a[i] >= b[i-1]) dp[i][j][0] |= dp[i-1][j-1][1];
if(b[i] >= a[i-1]) dp[i][j][1] |= dp[i-1][j][0];
if(b[i] >= b[i-1]) dp[i][j][1] |= dp[i-1][j][1];
}
}
int x = 2*n,y = n,z = 0;
if(dp[x][y][z] == 0) z = 1;
if(dp[x][y][z] == 0){
cout << -1 << endl;
return 0;
}
string s = "";
while(x){
if(z == 0){
assert(y > 0);
s += "A";
if(a[x] >= a[x-1] && dp[x-1][y-1][0]){
x--;
y--;
z = 0;
}
else{
assert(a[x] >= b[x-1] && dp[x-1][y-1][1]);
x--;
y--;
z = 1;
}
}
else{
s += "B";
if(b[x] >= a[x-1] && dp[x-1][y][0]){
x--;
z = 0;
}
else{
assert(b[x] >= b[x-1] && dp[x-1][y][1]);
x--;
z = 1;
}
}
}
reverse(s.begin(),s.end());
cout << s << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |