#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 |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
4 ms |
860 KB |
Output is correct |
4 |
Correct |
7 ms |
1116 KB |
Output is correct |
5 |
Correct |
15 ms |
1672 KB |
Output is correct |
6 |
Correct |
32 ms |
3164 KB |
Output is correct |
7 |
Correct |
47 ms |
4688 KB |
Output is correct |