# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
122993 | thebes | Swap (BOI16_swap) | C++14 | 751 ms | 3704 KiB |
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;
const int MN = 2e5+5;
int arr[MN], n, i;
int rec(int p,int v){
if(2*p>n) return p;
else if(2*p+1>n) return v<arr[2*p]?p:2*p;
if(v<arr[2*p]&&v<arr[2*p+1]) return p;
if(arr[2*p]<v&&arr[2*p]<arr[2*p+1]) return rec(2*p,v);
if(arr[2*p+1]<v&&arr[2*p+1]<arr[2*p]){
if(v<arr[2*p]) return min(rec(2*p,v),rec(2*p+1,v));
else if(rec(2*p,arr[2*p])<rec(2*p+1,arr[2*p])) return rec(2*p+1,v);
else return rec(2*p,v);
}
}
int main(){
for(scanf("%d",&n),i=1;i<=n;i++)
scanf("%d",&arr[i]);
for(i=1;i<=n;i++){
if(2*i>n) continue;
if(2*i+1>n){
if(arr[2*i]<arr[i]) swap(arr[2*i],arr[i]);
}
else{
if(arr[2*i+1]<arr[i]&&arr[2*i+1]<arr[2*i]){
int a = min(arr[i],arr[2*i]);
int b = max(arr[i],arr[2*i]);
arr[i] = arr[2*i+1];
if(rec(2*i,a)<rec(2*i+1,a)) arr[2*i]=a,arr[2*i+1]=b;
else arr[2*i+1]=a,arr[2*i]=b;
}
else if(arr[2*i]<arr[i]&&arr[2*i]<arr[2*i+1]) swap(arr[i],arr[2*i]);
}
}
for(i=1;i<=n;i++)
printf("%d ",arr[i]);
return 0;
}
Compilation message (stderr)
# | 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... |