#include <bits/stdc++.h>
using namespace std;
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define print(v,r) f(i,0,r) cout<<v[i]<<" "; cout<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define Add(x,y) x=(x+y)%mod
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
const int N=1e6+99;
int n,t,s[N],a[N],ans[N];
vector<int> g[N];
void Min(int &id,int x){
if(a[x]<a[id]) id=x;
}
bool is_par(int u,int v){
while(u<v) v/=2;
return (u==v);
}
int main(){
fill(s,s+N,-1);
a[0]=N;
cin>>n;
f(i,1,n+1){
cin>>a[i];
}
f(i,1,n+1){
int id=0,u=i;
if(s[i]!=1) id=i;
if(i*2<=n) Min(id,i*2+0);
if(i*2<n) Min(id,i*2+1);
while(u>1 && s[u]!=0){
if((u&1) && s[u^1]!=0){
Min(id,u^1);
}
if(s[u/2]!=1 && (u%2==0 || s[u^1]!=1)){
Min(id,u/2);
}
if((u&1) && s[u^1]==1){
break;
}
u/=2;
}
//cout<<i<<" "<<id<<endl;
if(id==i*2){
s[i*2+0]=1;
s[i*2+1]=0;
}
else if(id==i*2+1){
s[i*2+1]=1;
}
else if(i==id){
s[i]=0;
s[i*2+0]=0;
s[i*2+1]=0;
}
else if(is_par(id,i)){
int lastu=0;
s[id]=0;
s[i*2+0]=0;
s[i*2+1]=0;
u=i;
while(u!=id){
s[u]=1;
lastu=u;
u/=2;
}
if(lastu&1){
s[lastu^1]=0;
}
}
else{
s[i*2+0]=0;
s[i*2+1]=0;
u=i;
s[id]=1;
while(u>1 && u*2!=id){
s[u]=1;
if((u^1)!=id && (u&1)){
s[u^1]=0;
}
u/=2;
//eror(u);
if(u==0) exit(0);
}
}
ans[i]=a[id];
//eror(s[3]);
}
f(i,1,n+1){
cout<<ans[i]<<" ";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
27596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
27596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
27596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
27596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
27596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |