Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. //#define DEBUG
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <istream>
  6. #include <vector>
  7. #include <sstream>
  8. #include <utility>
  9. using namespace std;
  10.  
  11. vector<int> heap;
  12.  
  13. void sift_down(int i) {
  14.     unsigned left = i * 2 + 1;
  15.     unsigned right = i * 2 + 2;
  16.      
  17.     int smallest = i;
  18.  
  19.     if (left < heap.size() && heap[left] < heap[smallest])
  20.         smallest = left;
  21.  
  22.     if (right < heap.size() && heap[right] < heap[smallest])
  23.         smallest = right;
  24.  
  25.     if (smallest != i) {
  26.         swap(heap[smallest], heap[i]);
  27.         sift_down(smallest);
  28.     }
  29. }
  30.  
  31. void sift_up(int i) {
  32.     if (i == 0) return;
  33.  
  34.     int parent = (i - 1) / 2;
  35.     if (heap[i] < heap[parent]) {
  36.         swap(heap[i], heap[parent]);
  37.         sift_up(parent);
  38.     }
  39. }
  40.  
  41. void push(int val) {
  42.     heap.push_back(val);
  43.     sift_up(heap.size() - 1);
  44. }
  45.  
  46. int extract_min() {
  47.     swap(heap[0], heap[heap.size() - 1]);
  48.  
  49.     int val = heap.back();
  50.     heap.pop_back();
  51.  
  52.     sift_down(0);
  53.  
  54.     return val;
  55. }
  56.  
  57. bool empty() {
  58.     return heap.empty();
  59. }
  60.  
  61. void decrease_key(int key, int keyTo) {
  62.     int pos;
  63.  
  64.     for (pos = 0; heap[pos] != key; ++pos);
  65.      
  66.     heap[pos] = keyTo;
  67.     sift_up(pos);
  68. }
  69.  
  70.  
  71. // all solution is here, not in main function
  72. void resolve(istream &in, ostream &out) {
  73.     vector<int> ops;
  74.     string line;
  75.      
  76.     while (getline(in, line)) {
  77.         stringstream st(line);
  78.  
  79.         string command;
  80.         st >> command;
  81.  
  82.         if (command == "push") {
  83.             int val;
  84.             st >> val;
  85.  
  86.             push(val);
  87.  
  88.             ops.push_back(val);
  89.  
  90.         } else if (command == "extract-min") {
  91.             if (empty())
  92.                 out << '*' << endl;
  93.             else
  94.                 out << extract_min() << endl;
  95.  
  96.             ops.push_back(-1);
  97.  
  98.         } else if (command == "decrease-key") {
  99.             int from, to;
  100.  
  101.             st >> from >> to;
  102.  
  103.             decrease_key(ops[from - 1], to);
  104.             ops[from - 1] = to;
  105.  
  106.             ops.push_back(-1);
  107.         }
  108.     }
  109. }
  110.  
  111.  
  112.  
  113.  
  114. // for convinient testing and debugging
  115. string problem = "priorityqueue";
  116. #ifndef TEST
  117. int main() {
  118. #ifdef DEBUG
  119.     istream &in = cin;
  120.     ostream &out = cout;
  121. #else
  122.     string instring = problem + ".in";
  123.     string outstring = problem + ".out";
  124.     istream &in = *(new ifstream(instring.c_str()));
  125.     ostream &out = *(new ofstream(outstring.c_str()));
  126. #endif
  127.      
  128.     resolve(in, out);
  129.      
  130.     out.flush();
  131.     return 0;
  132. }
  133. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement