Advertisement
snowywhitee

Untitled

Nov 8th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <sstream>
  5. using namespace std;
  6.  
  7. int search(vector<int> &arr, int n, int k)
  8. {
  9.     int r,l,mid;
  10.     l = 0;
  11.     r = n;
  12.     while ((r - l) >= 1)
  13.     {
  14.         mid = (l + r) / 2;
  15.         if (k < arr[mid])
  16.         {
  17.             r = mid;
  18.         }else if (k > arr[mid])
  19.         {
  20.             l = mid;
  21.         }else if (k == arr[mid])
  22.         {
  23.             return mid;
  24.         }
  25.     }
  26. }
  27.  
  28. void Sink(vector<int> &arr, int k, int n)
  29. {
  30.     int placed = 0;
  31.     int j = 2*k+1;
  32.     while (placed == 0 && j <= (n-1))
  33.     {
  34.         if (j < (n - 1) && arr[j] < arr[j+1])
  35.         {
  36.             j = j + 1;
  37.         }
  38.         if (arr[k] >= arr[j])
  39.         {
  40.             placed = 1;
  41.         }else{
  42.             swap(arr[k], arr[j]);
  43.         }
  44.         k = j;
  45.         j = 2*k + 1;
  46.     }
  47. }
  48.  
  49. void HeapSort(vector<int> &arr, int n)
  50. {
  51.     for (int i = (n-1)/2; i > -1; i--)
  52.     {
  53.         Sink(arr, i, n);
  54.     }
  55.     while (n > 0)
  56.     {
  57.         swap(arr[0], arr[n - 1]);
  58.         n = n - 1;
  59.         Sink(arr, 0, n);
  60.     }
  61. }
  62.  
  63. void pop(vector<int> &arr)
  64. {
  65.     vector<int>::iterator it;
  66.     it = arr.begin();
  67.     arr.erase(it);
  68. }
  69. void push(vector<int> &arr, int d)
  70. {
  71.     arr.push_back(d);
  72.     HeapSort(arr, arr.size());
  73. }
  74.  
  75. void decrease_key(vector<int> &arr, int key, int m)
  76. {
  77.     vector<int>::iterator it;
  78.     it = arr.begin() + search(arr, arr.size(), key);
  79.     arr.erase(it);
  80.     push(arr, m);
  81. }
  82.  
  83.  
  84. int main()
  85. {
  86.     int *top = NULL;
  87.     int *tail = NULL;
  88.     std::vector<int> arr;
  89.    
  90.     ifstream fin("1.txt");
  91.     ofstream fout("priorityqueue.out");
  92.    
  93.     string command;
  94.     int num;
  95.     vector< pair<int,int> > ar;
  96.     int nomerstroki = 0;
  97.     while (fin.eof() != 1)
  98.     {
  99.         fin >> command;
  100.         nomerstroki++;
  101.         if (command == "push")
  102.         {
  103.             fin >> num;
  104.             push(arr, num);
  105.             pair<int,int> pair1;
  106.             pair1.first = nomerstroki;
  107.             pair1.second = num;
  108.             ar.push_back(pair1);
  109.         } else if (command == "extract-min")
  110.         {
  111.             if (arr.size() > 0)
  112.             {
  113.                 fout << arr.front() << endl;
  114.                 pop(arr);
  115.             } else {
  116.                 fout << "*" << endl;
  117.             }
  118.         } else if (command == "decrease-key")
  119.         {
  120.             int k,k1,m;
  121.             string s;
  122.             fin >> k >> m;
  123.             for(int i = 0; i < ar.size(); i++)
  124.             {
  125.                 if (k == ar[i].first)
  126.                 {
  127.                     k1 = ar[i].second;
  128.                 }
  129.             }
  130.             decrease_key(arr, k1, m);
  131.         }
  132.     }
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement