이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2000;
int a[2 * MAX_N + 1], b[2 * MAX_N + 1], dp[2 * MAX_N + 1][2 * MAX_N + 1][2];
int main() {
int n;
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] = true;
dp[0][0][1] = true;
for ( int i = 1; i <= 2 * n; i++ ) {
for ( int j = 1; j <= i; j++ ) {
if ( a[i] >= a[i - 1] )
dp[i][j][0] |= dp[i - 1][j - 1][0];
if ( a[i] >= b[i - 1] )
dp[i][j][0] |= dp[i - 1][j - 1][1];
}
for ( int j = 0; j <= i; j++ ) {
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];
}
}
if ( (dp[2 * n][n][0] | dp[2 * n][n][1]) == 0 ) {
cout << -1;
return 0;
}
vector<char> sol;
int j = n, lastt = 1e9;
for ( int i = 2 * n; i >= 1; i-- ) {
if ( dp[i][j][0] && a[i] <= lastt ) {
sol.push_back( 'A' );
lastt = a[i];
j--;
} else {
sol.push_back( 'B' );
lastt = b[i];
}
}
reverse( sol.begin(), sol.end() );
for ( char c: sol )
cout << c;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |