Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <cmath>
  5. using namespace std;
  6. ifstream in("priorityqueue.in");
  7. ofstream out("priorityqueue.out");
  8.  
  9.  
  10. struct Heap{
  11. int siz;
  12. vector<int> heap;
  13. vector<int> ind;
  14.  
  15. /* void siffUp(int v){
  16. int p;
  17. p = v-1 / 2;
  18. if( p == 0 )return ;
  19. if(heap[p] < heap[v])
  20. swap(heap[p],heap[v]);
  21. siffUp(p);
  22. }*/
  23. void siffUp(int v){ // работает.
  24. while(v>0){
  25. int p;
  26. p = (v) / 2;
  27. //if( p == 0 )return ;
  28. if(heap[p] > heap[v]){
  29. swap(heap[p],heap[v]);
  30. swap(ind[p],ind[v]);
  31. }
  32. else break;
  33. v=p;
  34. //siffUp(p);
  35. }}
  36.  
  37. add(int x){
  38. siz++;
  39. ind.push_back(siz);
  40. heap.push_back(x);
  41. // cout<<"Enter"<<endl;
  42. siffUp(heap.size() -1); //
  43. }
  44.  
  45.  
  46. void siffDown(int v){ // не работает
  47. //cout<<"Enter1"<<endl;
  48.  
  49. while(2*v <heap.size() ){
  50. int left = 2 * v ; // left — левый сын
  51. int right = 2 * v + 1;
  52. if(v==0){left++;right++;} // right — правый сын
  53. int j = left;
  54. if (right < heap.size() && heap[right] < heap[left])
  55. j = right;
  56. if (heap[v] <= heap[j])
  57. break;
  58. swap(heap[v], heap[j]);
  59. swap(ind[v],ind[j]);
  60. v = j;
  61. //for(int i=0;i<7;i++)cout<<heap[i]<<" ";
  62. //cout<<endl;
  63. }
  64. }
  65.  
  66. void decrease_key(){
  67. int a , b;
  68. in>>a>>b;
  69. for(int i=0;i<heap.size();i++){
  70. if(ind[i] == a){
  71. heap[i] = b;
  72. if(heap[i] >= heap[2*i] || heap[i] >= heap[2*i + 1]){ // ИЗМЕНИТЬ УСЛОВИЕ !!!!!!!!!
  73. siffDown(i);
  74. }
  75.  
  76. if (heap[i] < heap[i/2]) {
  77. siffUp(i);
  78. }
  79. }
  80. }
  81.  
  82. }
  83.  
  84.  
  85.  
  86.  
  87. int extractMin(){
  88. if(siz>0){
  89. int omega=heap[0];
  90. heap[0] = heap[heap.size()-1];
  91. ind [0] = ind [heap.size()-1];
  92. heap.pop_back();
  93. ind.pop_back();
  94. siz--;
  95. siffDown(0);
  96. return omega;
  97. }
  98.  
  99. if(siz<= 0) return 1000000001;
  100. }
  101.  
  102.  
  103.  
  104.  
  105. void heapify(){
  106. for(int i=heap.size() ;i>=0;i--)
  107. siffDown(i);
  108. }
  109. };
  110.  
  111.  
  112.  
  113. int main()
  114. {
  115.  
  116. bool k = 0;
  117.  
  118. Heap please;
  119. please.siz=0;
  120. vector<int> ans;
  121. while (!in.eof()){
  122. //cout<<1;
  123. string d;
  124. in>>d;
  125.  
  126. if(d == "push"){
  127. int element;
  128. in>>element;
  129. please.add(element);
  130.  
  131. }
  132. else if(d == "extract-min"){
  133. ans.push_back(please.extractMin());
  134. }
  135. else if(d == "decrease-key"){
  136. // int a , b;
  137. // cin>>a>>b;
  138. // for(int i=0;i<please.heap.size();i++){
  139. // if(please.ind[i] == a){
  140. // please.heap[i] = b;
  141.  
  142. please.decrease_key();
  143. }
  144.  
  145.  
  146. //else if(d == "lol")k=1;
  147. }
  148.  
  149. //for( int i = 0; i < please.heap.size() ; i++ ){
  150. // cout<<please.heap[i]<<endl;
  151. // }
  152. for(int i=0;i<ans.size();i++){
  153. if(ans[i] == 1000000001)
  154. out<<"*"<<endl;
  155. else
  156. out<<ans[i]<<endl;
  157. }
  158. //cout<<please.siz;
  159. //cout<<please.extractMin();
  160. // cout<<please.heap.size();
  161. //cout<<please.heap[0];
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement