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;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int mxn = 1e6+10;
int N;
int arr[mxn][2];
int ch[mxn];
int need,cnt;
bitset<mxn> done;
void chain(int s,int e){
if(!cnt)return;
//cout<<"CHAIN:"<<s<<' '<<e<<endl;
pii big = make_pair(0,e+1);
int sum = 0;
for(int i = e;i>=s;i--){
if(i != 1&&arr[i-1][ch[i]]>=arr[i][ch[i]^1])break;
if(i != N*2&&max(arr[i+1][0],arr[i+1][1])<arr[i][ch[i]^1])break;
sum += (ch[i] == need?-1:1);
if(sum == cnt)big = make_pair(mxn,i);
else big = max(big,make_pair(sum,i));
}
for(int i = e;i>=big.sc;i--){
ch[i] ^= 1;
}
cnt = max(0,cnt-big.fs);
return;
}
void calc(int s,int e){
//cout<<"calc:"<<s<<' '<<e<<endl;
if(!cnt)return;
vector<pii> v = {{s,s}};
for(int i = s+1;i<=e;i++){
//cout<<i<<":"<<arr[i-1][ch[i]^1]<<','<<arr[i][ch[i]]<<endl;
if(arr[i-1][ch[i-1]^1]<=arr[i][ch[i]])v.push_back({i,i});
else v.back().sc = i;
}
for(auto &i:v)chain(i.fs,i.sc);
}
void check(){
vector<pii> v;
bool flag = false;
for(int i = 1;i<=N*2;i++){
if(arr[i][ch[i]]>arr[i][ch[i]^1])flag = false;
else{
if(flag)v.back().sc = i;
else v.push_back({i,i});
flag = true;
}
}
for(auto &i:v)calc(i.fs,i.sc);
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N;
for(int i = 0;i<2;i++){
for(int j = 1;j<=N*2;j++)cin>>arr[j][i];
}
if(arr[1][0]>arr[1][1])ch[1] = 1;
for(int i = 2;i<=N*2;i++){
if(arr[i][1]<arr[i][0])ch[i] = 1;
if(arr[i][ch[i]]<arr[i-1][ch[i-1]])ch[i]^=1;
if(arr[i][ch[i]]<arr[i-1][ch[i-1]]){
cout<<"-1\n";
return 0;
}
}
int sum = 0;
for(int i = 1;i<=N*2;i++)sum += ch[i];
if(sum<N)need = 1,cnt = N-sum;
else need = 0,cnt = sum-N;
///for(int i = 1;i<=N*2;i++)cout<<ch[i];cout<<endl;
//cout<<need<<','<<cnt<<endl;
check();
if(cnt){
cout<<"-1";
return 0;
}
for(int i = 1;i<=N*2;i++){
cout<<"AB"[ch[i]];
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |