Advertisement
Guest User

Untitled

a guest
Dec 19th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <cassert>
  3. #include <ctime>
  4. #include <algorithm>
  5.  
  6. #define forn(i, n) for (int i = 0; i < int(n); i++)
  7.  
  8. using namespace std;
  9.  
  10. const int N = 10000000;
  11.  
  12. int h[N];
  13.  
  14. void pushDown(int pos, int n)
  15. {
  16.     while (2 * pos + 1 < n)
  17.     {
  18.         int j = 2 * pos + 1;
  19.         if (j + 1 < n && h[j + 1] > h[j])
  20.             j++;
  21.         if (h[pos] >= h[j])
  22.             break;
  23.         swap(h[pos], h[j]);
  24.         pos = j;
  25.     }
  26. }
  27.  
  28. int main()
  29. {
  30.     forn(i, N)
  31.         h[i] = i;
  32.        
  33.     random_shuffle(h, h + N);
  34.    
  35.     clock_t start = clock();
  36.  
  37.     for (int i = N / 2; i >= 0; i--)
  38.         pushDown(i, N);
  39.  
  40.     int n = N;
  41.     while (n > 1)
  42.     {
  43.         swap(h[0], h[n - 1]);
  44.         n--;
  45.         pushDown(0, n);
  46.     }
  47.  
  48.     forn(i, N)
  49.         assert(h[i] == i);
  50.  
  51.     cout << "Done in " << (clock() - start) * 1000 / CLOCKS_PER_SEC << endl;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement