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 pb push_back
using namespace std;
typedef vector<int> vim;
const int INF = 1<<20;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N; cin>>N; N*=2;
string S;
vim A(N), B(N), mxa(N), mna(N), mxb(N), mnb(N);
for (auto &i:A) cin>>i;
for (auto &i:B) cin>>i;
mxa[0]=mna[0]=1, mxb[0]=mnb[0]=0;
for (int i=1; i<N; i++) {
mxa[i]=mxb[i]=-INF;
mna[i]=mnb[i]=INF;
if (A[i-1]<=A[i]) mxa[i]=max(mxa[i], mxa[i-1]+1), mna[i]=min(mna[i], mna[i-1]+1);
if (A[i-1]<=B[i]) mxb[i]=max(mxb[i], mxa[i-1]+0), mnb[i]=min(mnb[i], mna[i-1]+0);
if (B[i-1]<=A[i]) mxa[i]=max(mxa[i], mxb[i-1]+1), mna[i]=min(mna[i], mnb[i-1]+1);
if (B[i-1]<=B[i]) mxb[i]=max(mxb[i], mxb[i-1]+0), mnb[i]=min(mnb[i], mnb[i-1]+0);
}
int c;
if (mna[N-1]<=N/2&&N/2<=mxa[N-1]) S.pb('A'), c=1;
else if (mnb[N-1]<=N/2&&N/2<=mxb[N-1]) S.pb('B'), c=0;
else { cout<<"-1\n"; return 0; }
for (int i=N-2; i>=0; i--) {
if (S.back()=='A') {
if (A[i]<=A[i+1]&&mna[i]<=N/2-c&&N/2-c<=mxa[i]) S.pb('A'), c++;
else if (B[i]<=A[i+1]&&mnb[i]<=N/2-c&&N/2-c<=mxb[i]) S.pb('B');
}
else {
if (A[i]<=B[i+1]&&mna[i]<=N/2-c&&N/2-c<=mxa[i]) S.pb('A'), c++;
else if (B[i]<=B[i+1]&&mnb[i]<=N/2-c&&N/2-c<=mxb[i]) S.pb('B');
}
}
reverse(S.begin(), S.end());
cout<<S<<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |