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 int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n;
int a[1000005];
int b[1000005];
pii memo[1000005][2];
pii dp(int index, bool amos){
if(index>2*n){
return memo[index][amos]={0,0};
}
if(memo[index][amos].first!=-1) return memo[index][amos];
pii ans={INT_MAX,INT_MIN};
if(amos){
if(a[index]>=a[index-1]){
pii hold=dp(index+1,true);
if(hold.first<=hold.second){
ans.first=min(ans.first,hold.first+1);
ans.second=max(ans.second,hold.second+1);
}
}
if(b[index]>=a[index-1]){
pii hold=dp(index+1,false);
if(hold.first<=hold.second){
ans.first=min(ans.first,hold.first);
ans.second=max(ans.second,hold.second);
}
}
}
else{
if(a[index]>=b[index-1]){
pii hold=dp(index+1,true);
if(hold.first<=hold.second){
ans.first=min(ans.first,hold.first+1);
ans.second=max(ans.second,hold.second+1);
}
}
if(b[index]>=b[index-1]){
pii hold=dp(index+1,false);
if(hold.first<=hold.second){
ans.first=min(ans.first,hold.first);
ans.second=max(ans.second,hold.second);
}
}
}
return memo[index][amos]=ans;
}
void dfs(int index, bool amos, int need){
if(index>2*n) return;
pii take={INT_MAX,INT_MIN};
pii take2={INT_MAX,INT_MIN};
if(amos){
if(a[index]>=a[index-1]){
take=memo[index+1][true];
take.first++;
take.second++;
}
if(b[index]>=a[index-1]){
take2=memo[index+1][false];
}
}
else{
if(a[index]>=b[index-1]){
take=memo[index+1][true];
take.first++;
take.second++;
}
if(b[index]>=b[index-1]){
take2=memo[index+1][false];
}
}
if(take.first<=need&&take.second>=need){
cout << "A";
dfs(index+1,true,need-1);
}
else{
cout << "B";
dfs(index+1,false,need);
}
}
void solve(){
cin >> n;
for(int x=1;x<=2*n;x++){
cin >> a[x];
}
for(int x=1;x<=2*n;x++){
cin >> b[x];
}
memset(memo,-1,sizeof(memo));
pii hold=dp(1,0);
if(hold.first<=n&&hold.second>=n){
dfs(1,0,n);
}
else{
cout << -1;
}
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |