-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxheap.cpp
66 lines (58 loc) · 950 Bytes
/
maxheap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<vector>
#include<bits/stdc++.h>
#define lli long long int
#define mod 1000000007
#define pb push_back
#define mk make_pair
#define fastio ios_base::sync_with_stdio(false); cout.tie(NULL);
using namespace std;
int a[100001],x,sorted;
void makeHEAP(int n)
{
int i,j;
while(sorted<x)
{
sorted=0;
for(i=x;i>=1;i--)
{
if(2*i<=n && 2*i+1<=n)
{
if(a[i]<a[2*i] || a[i]<a[(2*i)+1]) // all elements are distinct
{
if(a[2*i]>a[(2*i)+1])
swap(a[i],a[2*i]);
else
swap(a[i],a[(2*i)+1]);
}
}
else if(2*i<=n && 2*i+1>n)
{
if(a[2*i]>a[i])
swap(a[i],a[2*i]);
}
}
for(i=x;i>=1;i--)
if(a[i]>a[2*i] && a[i]>a[(2*i)+1])
sorted++;
}
}
int main()
{
fastio;
int n,i,j;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
if(i>3)
{
cout<<"YES\n";
x=i/2;
makeHEAP(i);
cout<<"YES1 "<<i<<endl;
for(j=1;j<=i;j++)
cout<<a[j]<<" ";
}
}
}