Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- int h[500003];
- int i , n , k;
- void heapup( int i )
- {
- if (i*2>1)
- if (h[i] < h[i/2])
- {
- swap( h[i] , h[i/2] );
- heapup( i/2 );
- }
- }
- void heapdown( int i )
- {
- if (i*2<=n)
- {
- if (i*2+1<=n && h[i*2]>h[i*2+1] && h[i]>h[i*2+1] )
- {
- swap(h[i*2+1],h[i]);
- heapdown( i*2+1 );
- }
- else if (h[i] > h[i*2] )
- {
- swap( h[i] , h[i*2] );
- heapdown( i*2 );
- }
- }
- }
- int main()
- {
- cin>>n;
- for (i=1; i<=n; i++)
- {
- cin>>h[i];
- heapup(i);
- }
- k=n;
- for (i=1; i<k; i++)
- {
- swap(h[1],h[n]);
- n--;
- heapdown( 1 );
- }
- for(i=k; i>=1; i--) cout<<h[i]<<" ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement