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>
#define T(a,b,c) make_tuple(a,b,c)
using namespace std;
int arr[400100],n;
int dp(int i,int v){
if(i*2>n)
return 0;
int x = arr[2*i];
if(i*2==n)
return x<v;
int y=arr[2*i+1];
if(v<x&&v<y)
return 0;
if(x<v&&x<y)
return 1 + dp(2*i,v);
if(v<x)
return min(dp(2*i,v),dp(2*i+1,v))+1;
int l1 = dp(2*i,x), l2 = dp(2*i+1,x);
if(l1<=l2)
return 1 +dp(2*i+1,v);
return 1+dp(2*i,v);
}
int main() {
memset(arr, 7, sizeof arr);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> arr[i];
for(int i = 1; i <= n; i++) {
int &a=arr[i],&b=arr[i*2],&c=arr[i*2+1];
if(a<b&&a<c);
else if(b<a&&b<c)
swap(a,b);
else {
swap(a,c);
if(b>c)
swap(b,c);
if(dp(2*i,b)>dp(2*i+1,b)) swap(b,c);
}
cout << a << ' ';
}
}
# | 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... |