이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define fi first
#define se second
#define rep(a, b) for(int a = 0; a < (b); a++)
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<ll> vl;
const int N=1e6+10;
pair<int,int> dp[N][2];
int t1[N],t2[N];
void solve(){
int n;
cin>>n;
n*=2;
rep(i,n) cin>>t1[i];
rep(i,n) cin>>t2[i];
dp[0][0]={1,1};
dp[0][1]={0,0};
for(int i=1;i<n;i++){
dp[i][0]={-1,-1},dp[i][1]={-1,-1};
pair<int,int> p1={1000*1000+10,0};
if(t1[i]>=t1[i-1] and dp[i-1][0].fi!=-1) p1=dp[i-1][0];
if(t1[i]>=t2[i-1] and dp[i-1][1].fi!=-1) p1.fi=min(p1.fi,dp[i-1][1].fi),p1.se=max(p1.se,dp[i-1][1].se);
if(p1.fi!=1000*1000+10) dp[i][0]={p1.fi+1,p1.se+1};
p1={1000*1000+10,0};
if(t2[i]>=t1[i-1] and dp[i-1][0].fi!=-1) p1=dp[i-1][0];
if(t2[i]>=t2[i-1] and dp[i-1][1].fi!=-1) p1.fi=min(p1.fi,dp[i-1][1].fi),p1.se=max(p1.se,dp[i-1][1].se);
if(p1.fi!=1000*1000+10) dp[i][1]=p1;
}
if((dp[n-1][0].fi>n/2 or dp[n-1][0].se<n/2) and (dp[n-1][1].fi>n/2 or dp[n-1][1].se<n/2)){cout<<-1<<"\n";return;}
int curr=n/2,j=n-1,pop=1000*1000*1000+10;
string s="";
while(j>=0){
if(pop>=t1[j] and dp[j][0].se>=curr and dp[j][0].fi<=curr){
pop=t1[j],curr--;
s+='A';
}else{
s+='B';
pop=t2[j];
}
j--;
}
reverse(s.begin(),s.end());
cout<<s<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |