Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int MAXN = 1000000;
- int a[MAXN],sz=1;
- void go_up(int v)
- { int i=v/2;
- while(i>0&&a[i]>a[v])
- { int t=a[v];a[v]=a[i];a[i]=t;
- v=i;i=i/2;
- }
- }
- void go_down(int v)
- { int i=v,t;
- if(2*v<=sz&&a[v]>a[2*v]) i=2*v;
- if(2*v+1<=sz&&a[i]>a[2*v+1])i=2*v+1;
- if(i!=v)
- { t=a[v];a[v]=a[i];a[i]=t;go_down(i);
- }
- }
- int main(){
- int que;
- while( cin >> que ){
- if( que == -1 ) { cout << endl; break; }
- if( que == 0 ) {
- cout << a[1] << " ";
- a[1] = a[--sz];
- go_down(1);
- } else {
- a[sz] = que;
- go_up( sz++ );
- }
- }
- //for( int i=1; i<sz; i++ ) cout << a[i] << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment