Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. #include <conio.h>
  4. using namespace std;
  5.  
  6. const int NMAX=1000;
  7.  
  8. int DHeap[NMAX];
  9.  
  10. int n=0,d;
  11. ///O(log(d->n))
  12. void FixUp(int x)
  13. {
  14. int p=(x-1)/d;
  15. while(x!=0 && DHeap[p]>DHeap[x]) {
  16. swap(DHeap[p],DHeap[x]);
  17. x=p;
  18. p=(n-1)/d;
  19. }
  20. };
  21.  
  22. void Add() {
  23. int x;
  24. cin>>x;
  25. DHeap[n]=x;
  26. FixUp(n);
  27. n++;
  28. };
  29.  
  30. void Output() {
  31. cout<<DHeap[0];
  32. for(int i=1; i<n; ++i) {
  33. if(i%d==1) cout<<endl;
  34. cout<<DHeap[i]<<' ';
  35. }
  36. cout<<endl;
  37. system("pause");
  38. };
  39.  
  40. int minchild(int x) {
  41. if(x*d+1>=n) return -1;
  42. int lastson=(x+1)*d,minson=x*d+1;
  43. if(lastson>=n) lastson=n-1;
  44. int firstson=x*d+1;
  45. for(int i=firstson; i<=lastson; ++i) {
  46. if(DHeap[i]<DHeap[minson])
  47. minson=i;
  48. }
  49. return minson;
  50.  
  51. }
  52. ///O(d*log(d->n))
  53. void FixDown(int x) {
  54. int mi=minchild(x);
  55. if(mi!=-1) {
  56. //cout<<x<<' '<<mi<<endl;
  57. if(x<n && DHeap[x]>DHeap[mi]) {
  58. swap(DHeap[x],DHeap[mi]);
  59. FixDown(mi);
  60. }
  61. }
  62. };
  63.  
  64. void Increase_key() {
  65. int x,i;
  66. cin>>i>>x;
  67. DHeap[i]+=x;
  68. FixDown(i);
  69.  
  70. };
  71. ///O(log(d->n)+n-n/d)==O(n)
  72. void Extract_Max() {
  73. int maxi=0;
  74. int pow=1,leaf=0;
  75. while(leaf<n) {
  76. pow*=d;
  77. leaf+=pow;
  78. }
  79. for(int i=leaf-pow; i<n; ++i) {
  80. if(DHeap[i]>DHeap[maxi]) maxi=i;
  81. }
  82. cout<<DHeap[maxi];
  83. system("pause");
  84. };
  85.  
  86.  
  87. int MainMenu() {
  88. int key = 0;
  89. int code;
  90. do {
  91. system("cls");
  92. key = (key + 5) % 5;
  93. if (key == 0) cout << "-> Add next num" << endl;
  94. else cout << " Add next num" << endl;
  95. if (key == 1) cout << "-> Output matrix" << endl;
  96. else cout << " Output matrix" << endl;
  97. if (key == 2) cout << "-> Extract_Max" << endl;
  98. else cout << " Extract_Max" << endl;
  99. if (key == 3) cout << "-> Increase_Key" << endl;
  100. else cout << " Increase_Key" << endl;
  101. if (key == 4) cout << "-> Exit" << endl;
  102. else cout << " Exit" << endl;
  103. code = _getch();
  104. if (code == 224)
  105. {
  106. code = _getch();
  107. if (code == 80) key++;
  108. if (code == 72) key--;
  109. }
  110. } while (code != 13);
  111. system("cls");
  112. return key;
  113. }
  114.  
  115. int main()
  116. {
  117. cin>>d;
  118. for (;;) {
  119. int answer = MainMenu();
  120. switch (answer)
  121. {
  122. case 0: Add(); break;
  123. case 1: Output(); break;
  124. case 2: Extract_Max(); break;
  125. case 3: Increase_key(); break;
  126. case 4: return 0;
  127. }
  128. }
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement