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 F first
#define S second
#define int long long
pair<int, int> uni(pair<int, int> p1, pair<int, int> p2){
return {min(p1.F, p2.F), max(p1.S, p2.S)};
}
const int N =1e6+3;
int a[N], b[N];
pair<int, int> dp[N][2];
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
n<<=1;
for(int i=0; i< n; i++)cin >> a[i];
for(int i=0; i< n; i++)cin >> b[i];
dp[0][0]={1, 1};
dp[0][1]={0, 0};
for(int i=1; i<n; i++){
dp[i][0]=dp[i][1]={1e9, -1e9};
if(a[i]>=a[i-1] && a[i]>=b[i-1]){
dp[i][0]=uni(dp[i-1][0], dp[i-1][1]);
}
else if(a[i]>=a[i-1]){
dp[i][0]=dp[i-1][0];
}
else if(a[i]>=b[i]){
dp[i][0]=dp[i-1][1];
}
dp[i][0].F++;
dp[i][0].S++;
if(b[i]>=a[i-1] && b[i]>=b[i-1]){
dp[i][1]=uni(dp[i-1][0], dp[i-1][1]);
}
else if(b[i]>=a[i-1])dp[i][1]=dp[i-1][0];
else if(b[i]>=b[i-1])dp[i][1]=dp[i-1][1];
if(dp[i][0].F<0 && dp[i][1].F<0){
cout << -1 << '\n';
return 0;
}
}
pair<int, int> ans;
bool let=0;
string cons="";
int cnt=n/2;
if(dp[n-1][0].F<=n/2 && dp[n-1][0].S>=n/2){
ans=dp[n-1][0];
cnt--;
cons+='A';
}
else if(dp[n-1][1].F<=n/2 && dp[n-1][1].S>=n/2){
ans=dp[n-1][1];
let=1;
cons+='B';
}
else{
cout << -1 << '\n';
return 0;
}
for(int i=n-2; i>=0; i--){
if(let)swap(a[i+1], b[i+1]);
if(a[i+1]>=a[i] && dp[i][0].F<=cnt && dp[i][0].S>=cnt){
if(let)swap(a[i+1], b[i+1]);
cnt--;
let=0;
cons+='A';
continue;
}
if(let)swap(a[i+1], b[i+1]);
let=1;
cons+='B';
}
reverse(cons.begin(), cons.end());
cout << cons << '\n';
int cnta=0;
int prev=0;
for(int i=0; i< cons.size(); i++){
if(cons[i]=='A'){
if(a[i]<prev){
prev=2e9;
}
else prev=a[i];
cnta++;
}
else{
if(b[i]<prev){
prev=2e9;
}
else prev=b[i];
}
}
if(cons.size()!=n || prev==2e9 || cnta!=n/2){
assert(0);
}
}
Compilation message (stderr)
building4.cpp: In function 'int main()':
building4.cpp:87:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for(int i=0; i< cons.size(); i++){
| ~^~~~~~~~~~~~~
building4.cpp:102:19: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
102 | if(cons.size()!=n || prev==2e9 || cnta!=n/2){
| ~~~~~~~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |