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>
#include<fstream>
using namespace std;
#define ll long long
#define pb push_back
#define sz(x) int(x.size())
typedef pair<int,int>ii;
typedef vector<int> vi;
//we try to guess how many A's we will use on the building
//aparently (need to prove) the possibilities are contiguous
//i just need to find the minimum and maximum possible and check if n it is in it
const int mxn=1e6+5;
int n,a[2][mxn];
int dpmin[mxn][2],dpmax[mxn][2];
int smin(int i,int last){
if(i==0)return 0;
if(dpmin[i][last]!=-1)return dpmin[i][last];
int ans=1e9+5;
for(int j=0;j<2;j++){
if(a[j][i]<=a[last][i+1])ans=min(ans,smin(i-1,j)+j);
}
return dpmin[i][last]=ans;
}
int smax(int i,int last){
if(i==0)return 0;
if(dpmax[i][last]!=-1)return dpmax[i][last];
int ans=0;
for(int j=0;j<2;j++){
if(a[j][i]<=a[last][i+1])ans=max(ans,smax(i-1,j)+j);
}
return dpmax[i][last]=ans;
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;n*=2;
for(int i=1;i<=n;i++)cin>>a[0][i];
for(int i=1;i<=n;i++)cin>>a[1][i];
a[0][n+1]=a[1][n+1]=1e9+5;
memset(dpmin,-1,sizeof(dpmin));
memset(dpmax,-1,sizeof(dpmax));
int x=n/2,y=n/2,last=0;
string s;
for(int i=n;i>=1;i--){
if(smin(i,last)<=y && y<=smax(i,last)){
int mn=smin(i-1,0),mx=smax(i-1,0);
if(mn<=y && y<=mx && x && a[0][i]<=a[last][i+1]){
x--;
s+='A';
last=0;
}
else{
y--;
s+='B';
last=1;
}
}
else{
cout<<-1<<"\n";
return 0;
}
}
reverse(s.begin(),s.end());
if(x || y)cout<<-1<<"\n";
else cout<<s<<"\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |