# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
942656 | guagua0407 | Building 4 (JOI20_building4) | C++17 | 1 ms | 604 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int inf=1e9;
int main() {_
int n;
cin>>n;
n*=2;
vector<vector<int>> vec(n,vector<int>(2));
for(int i=0;i<n;i++){
cin>>vec[i][0];
}
for(int i=0;i<n;i++){
cin>>vec[i][1];
}
vector<int> dir(n);
for(int i=0;i<n;i++){
if(vec[i][0]>vec[i][1]){
swap(vec[i][0],vec[i][1]);
dir[i]=1;
}
}
vector<int> ans(n);
ans[0]=dir[0];
for(int i=1;i<n;i++){
int prv=vec[i-1][ans[i-1]^dir[i-1]];
if(prv<=vec[i][0]) ans[i]=dir[i];
else if(prv<=vec[i][1]) ans[i]=dir[i]^1;
else{
cout<<-1<<'\n';
return 0;
}
}
int cur=0;
for(int i=0;i<n;i++){
if(ans[i]==0) cur++;
}
//cout<<cur<<'\n';
for(int r=n-1;cur!=n/2 and r>=0;r--){
int l=r-1;
while(l>=0 and (vec[l][0]<=vec[l+1][0] and vec[l][1]<=vec[l+1][1] and vec[l][1]>vec[l+1][0])){
l--;
}
l++;
//cout<<l<<' '<<r<<'\n';
bool ok1=(l==0 or vec[l-1][ans[l-1]^dir[l-1]]<=vec[l][0]);
bool ok2=(r==n-1 or vec[r][1]<=vec[r+1][ans[r+1]^dir[r+1]]);
if(!ok1 or !ok2){
r=l;
continue;
}
for(int i=l;i<=r;i++){
assert(ans[i]==dir[i]);
}
vector<pair<int,int>> dif;
dif.push_back({0,r+1});
for(int i=r;i>=l;i--){
dif.push_back({dif.back().f+(dir[i]==0?-1:1),i});
}
/*reverse(all(dif));
for(auto v:dif){
cout<<v.f<<' '<<v.s<<'\n';
}*/
sort(all(dif));
int pos;
if(cur<=n/2){
pos=upper_bound(all(dif),make_pair(n/2-cur,inf))-dif.begin()-1;
}
else{
pos=lower_bound(all(dif),make_pair(n/2-cur,inf))-dif.begin();
}
int b=dif[pos].s;
cur+=dif[pos].f;
for(int i=r;i>=b;i--){
ans[i]=dir[i]^1;
}
r=l;
}
if(cur!=n/2){
assert(false);
cout<<-1<<'\n';
return 0;
}
int sum=(ans[0]==0);
for(int i=1;i<n;i++){
assert(vec[i-1][ans[i-1]^dir[i-1]]<=vec[i][ans[i]^dir[i]]);
sum+=(ans[i]==0);
}
assert(sum==n/2);
for(int i=0;i<n;i++){
cout<<(ans[i]==0?'A':'B');
}
cout<<'\n';
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |