#include "bits/stdc++.h"
using namespace std;
#ifdef LOCAL
#include "debug.cpp"
#else
#define debug(...)
#define debugArr(...)
#endif
#define int long long
const int mod = 1000000007;
signed main(){
ios_base::sync_with_stdio(false);cin.tie(nullptr);
int n;
cin>>n;
int a[n+1];
for(int i = 1;i<=n;i++){
cin>>a[i];
}
for(int i = 2;i<=n;i+=2){
if(i==n){
if(a[i]<a[i/2])swap(a[i],a[i/2]);
continue;
}
if(a[i/2]<a[i]&&a[i]<a[i+1])continue;
if(a[i/2]<a[i+1]&&a[i+1]<a[i])continue;
if(a[i]<a[i/2]&&a[i/2]<a[i+1])swap(a[i],a[i/2]);
if(a[i+1]<a[i/2]&&a[i/2]<a[i]){
if((i*2>n||a[i*2]>=a[i/2])&&(i*2+1>n||a[i*2+1]>=a[i/2]))swap(a[i],a[i/2]);
swap(a[i+1],a[i/2]);
}
if(a[i]<a[i+1]&&a[i+1]<a[i/2])swap(a[i],a[i/2]);
if(a[i+1]<a[i]&&a[i]<a[i/2]){
if(!((i*2>n||a[i*2]>=a[i/2])&&(i*2+1>n||a[i*2+1]>=a[i/2]))){
swap(a[i],a[i/2]);
}
swap(a[i+1],a[i/2]);
}
}
for(int i = 1;i<=n;i++)cout<<a[i]<<" ";
}
/*
[1,j]
[]
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |