VasilM

server_binary_pyramid

Jan 3rd, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. const int MAXN = 1000000;
  5.  
  6. int a[MAXN],sz=1;
  7.  
  8. void  go_up(int v)
  9. { int i=v/2;
  10.   while(i>0&&a[i]>a[v])
  11.   { int t=a[v];a[v]=a[i];a[i]=t;
  12.     v=i;i=i/2;
  13.   }
  14. }
  15.  
  16. void  go_down(int v)
  17. { int i=v,t;
  18.   if(2*v<=sz&&a[v]>a[2*v]) i=2*v;
  19.   if(2*v+1<=sz&&a[i]>a[2*v+1])i=2*v+1;
  20.   if(i!=v)
  21.   { t=a[v];a[v]=a[i];a[i]=t;go_down(i);
  22.   }
  23. }
  24.  
  25.  
  26. int main(){
  27.     int que;
  28.     while( cin >> que ){
  29.         if( que == -1 ) { cout << endl; break; }
  30.         if( que == 0 ) {
  31.             cout << a[1] << " ";
  32.             a[1] = a[--sz];
  33.             go_down(1);
  34.         } else {
  35.             a[sz] = que;
  36.             go_up( sz++ );
  37.         }
  38.     }
  39.     //for( int i=1; i<sz; i++ ) cout << a[i] << endl;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment