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>
#define x first
#define y second
#define pb push_back
using namespace std;
using pii=pair <int,int>;
pii merge(pii a,pii b){
if (a.x==-1) return b;
if (b.x==-1) return a;
return {min(a.x,b.x),max(a.y,b.y)};
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin>>n;
int a[2][2*n+1];
for (int i=0; i<2; i++){
for (int j=1; j<=2*n; j++) cin>>a[i][j];
}
pii dp[2][2*n+1];
for (int i=0; i<2; i++){
for (int j=1; j<=2*n; j++) dp[i][j]={-1,-1};
}
dp[0][1]={0,0}; dp[1][1]={1,1};
for (int j=2; j<=2*n; j++){
for (int i=0; i<2; i++){
for (int k=0; k<2; k++){
if (a[k][j-1]<=a[i][j]){
if (dp[k][j-1].x!=-1) dp[i][j]=merge(dp[i][j],{dp[k][j-1].x+i,dp[k][j-1].y+i});
}
}
}
}
int lst=-1;
if (dp[0][2*n].x<=n&&n<=dp[0][2*n].y) lst=0;
if (dp[1][2*n].x<=n&&n<=dp[1][2*n].y) lst=1;
if (lst==-1) {
cout<<"-1\n";
return 0;
}
string ans="";
int bal=n;
for (int i=2*n; i>0; i--){
if (!lst) ans+='A';
else {
ans+='B';
bal--;
}
if (i==1) break;
if (!bal||dp[0][i-1].x<=bal&&bal<=dp[0][i-1].y&&a[0][i-1]<=a[ans.back()-'A'][i]) lst=0;
else lst=1;
}
reverse(ans.begin(),ans.end());
cout<<ans<<'\n';
}
Compilation message (stderr)
building4.cpp: In function 'int main()':
building4.cpp:50:55: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
50 | if (!bal||dp[0][i-1].x<=bal&&bal<=dp[0][i-1].y&&a[0][i-1]<=a[ans.back()-'A'][i]) lst=0;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |