Submission #519387

#TimeUsernameProblemLanguageResultExecution timeMemory
519387new_acc건물 4 (JOI20_building4)C++14
100 / 100
220 ms45324 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...