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;
int n,arr[200005];
int cari(int i,int a){
if(i*2>n)return 0;
int d=arr[2*i];
if(i*2+1>n)return d<a;
int e=arr[2*i+1];
if(a<d && a<e)return 0;
if(d<a && d<e)return 1+cari(2*i,a);
if(a<d){
return min(cari(2*i,a),cari(2*i+1,a))+1;
}
int l1=cari(2*i,d),l2=cari(2*i+1,d);
if(l1<=l2)return 1+cari(2*i+1,a);
else return 1+cari(2*i,a);
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin >> n;
for(int i=1;i<=n;i++)cin >> arr[i];
arr[n+1]=1e9;
for(int i=1;2*i<=n;i++){
int a=arr[i],b=arr[2*i],c=arr[2*i+1];
if(a<b && a<c)continue;
if(b<a && b<c){
swap(arr[i],arr[2*i]);
continue;
}
if(a>b)swap(a,b);
if(cari(2*i,a)>cari(2*i+1,a))swap(a,b);
arr[i]=c;
arr[2*i]=a;
arr[2*i+1]=b;
}
for(int i=1;i<=n;i++){
cout << arr[i];
if(i<n)cout << " ";
else cout << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |