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