Na2a

Untitled

Feb 7th, 2017
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <climits>
  3.  
  4. using namespace std;
  5.  
  6. struct T {
  7. T(int val) : val(val) { }
  8. T(T* a, T* b, bool* d) : val(0), a(a), b(b), d(d) { }
  9.  
  10. int val;
  11. T* a;
  12. T* b;
  13. bool* d;
  14.  
  15. int query() {
  16. if(val) {
  17. return val;
  18. } else if(*d) {
  19. return a->query();
  20. } else {
  21. return min(a->query(), b->query());
  22. }
  23. }
  24. bool del(int x) {
  25. if(val) {
  26. if(val == x) {
  27. val = INT_MAX;
  28. return true;
  29. }
  30. return false;
  31. } else if(*d) {
  32. return a->del(x);
  33. } else {
  34. if(a->del(x)) {
  35. *d = true;
  36. return true;
  37. }
  38. if(b->del(x)) {
  39. *d = true;
  40. swap(*a, *b);
  41. return true;
  42. }
  43. return false;
  44. }
  45. }
  46. };
  47.  
  48. T* t[202020];
  49.  
  50. int main() {
  51. cin.sync_with_stdio(false);
  52. cin.tie(nullptr);
  53.  
  54. int n;
  55. cin >> n;
  56. for(int i = 1; i <= n; ++i) {
  57. int x;
  58. cin >> x;
  59. t[i] = new T(x);
  60. }
  61.  
  62. for(int i = 2; i <= n; ++i) {
  63. T* a = t[i / 2];
  64. T* b = t[i];
  65. bool* d = new bool(false);
  66. t[i / 2] = new T(a, b, d);
  67. t[i] = new T(b, a, d);
  68. }
  69.  
  70. for(int i = 1; i <= n; ++i) {
  71. int val = t[i]->query();
  72. t[i]->del(val);
  73. if(i != 1) cout << ' ';
  74. cout << val;
  75. }
  76. cout << '\n';
  77.  
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment