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;
const int nx=2e5+5;
int n, b[nx], a[nx], vs[nx], l, r, used[nx], cur=2;
struct fenwick
{
int d[nx];
void update(int i) {while (i<nx) d[i]++, i+=(i&-i);}
int query(int i)
{
int res=0;
while (i>0) res+=d[i], i-=(i&-i);
return res;
}
} f;
void getl()
{
while (vs[l]) l++;
}
void getr()
{
while (vs[r]) r--;
}
int main()
{
//cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>b[i], vs[b[i]]=1;
l=1, r=2*n-1;
a[1]=b[1];
used[a[1]]=1;
f.update(a[1]);
for (int i=3; i<2*n; i++)
{
//cout<<"debug "<<i<<' '<<cur<<'\n';
//for (int j=1; j<cur; j++) cout<<a[j]<<' ';
//cout<<'\n';
int idx=(i+1)/2;
if (!used[b[idx]]) used[b[idx]]=1, f.update(b[idx]), a[cur++]=b[idx];
while (f.query(b[idx]-1)!=f.query(2*n)-f.query(b[idx])||f.query(b[idx])!=idx)
{
if (f.query(b[idx]-1)<=f.query(2*n)-f.query(b[idx])) getl(), vs[l]=1, a[cur++]=l, f.update(l);
else getr(), vs[r]=1, a[cur++]=r, f.update(r);
}
}
for (int i=1; i<2*n; i++) cout<<a[i]<<' ';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |