#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=998244353;
int inf=1000000007;
ll infl=1000000000000000007;
const int N=200007;
int p[4*N];
int n;
int calc(int v,int x,int i)
{
if(v>n) return inf;
vector<int>V={x,p[2*v],p[2*v+1]};
sort(all(V));
if(x==V[0]) return i;
else if(p[2*v]==V[0]) return calc(2*v,x,i+1);
else return min(calc(2*v,x,i+1),calc(2*v+1,x,i+1));
}
void build(int v)
{
if(v>n) return ;
vector<int>V={p[v],p[2*v],p[2*v+1]};
//cout<<v<<" "<<p[v]<<" "<<p[2*v]<<" "<<p[2*v+1]<<endl;
sort(all(V));
if(p[v]==V[0])
{
if(p[2*v]!=inf&&p[2*v+1]!=inf)
{
if(calc(2*v,V[1],0)<=calc(2*v+1,V[2],0))
{
p[2*v]=V[1];
p[2*v+1]=V[2];
}
else
{
p[2*v]=V[2];
p[2*v+1]=V[1];
}
}
}
else if(p[2*v]==V[0]) swap(p[2*v],p[v]);
else
{
swap(p[2*v+1],p[v]);
if(p[2*v]!=inf&&p[2*v+1]!=inf)
{
if(calc(2*v,V[1],0)<=calc(2*v+1,V[2],0))
{
p[2*v]=V[1];
p[2*v+1]=V[2];
}
else
{
p[2*v]=V[2];
p[2*v+1]=V[1];
}
}
}
build(2*v);
build(2*v+1);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>p[i];
for(int i=n+1;i<=4*n;i++) p[i]=inf;
build(1);
for(int i=1;i<=n;i++) cout<<p[i]<<" ";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |