Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <cstdlib>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. vector<int> split(const string& s, char delim) {
  11.     vector<int> result;
  12.     stringstream ss(s);
  13.     unsigned element = 0;
  14.     string item;
  15.  
  16.     while (getline(ss, item, delim)) {
  17.         element++;
  18.         if (!item.empty() && element != 1) {
  19.             result.push_back(stoi(item));
  20.         }
  21.     }
  22.  
  23.     return result;
  24. }
  25.  
  26. int main() {
  27.     vector<pair<int, int>> vec;
  28.     ifstream fin("priorityqueue.in");
  29.     ofstream fout("priorityqueue.out");
  30.  
  31.     string line;
  32.     unsigned count = 0;
  33.  
  34.     while (getline(fin, line)) {
  35.         count++;
  36.  
  37.         if (line.find("push") != string::npos) {
  38.             int number = split(line, ' ')[0];
  39.             vec.push_back(pair<int, int>(count, number));
  40.             sort(vec.begin(), vec.end(), [](pair<int, int> a,
  41.                                             pair<int, int> b) {
  42.                 return a.second < b.second;
  43.             });
  44.         } else if (line.find("extract-min") != string::npos) {
  45.             if (vec.size() == 0) {
  46.                 fout << "*" << endl;
  47.             } else {
  48.                 fout << vec[0].second << endl;
  49.                 vec.erase(vec.begin());
  50.             }
  51.         } else if (line.find("decrease-key") != string::npos) {
  52.             int index = split(line, ' ')[0];
  53.             int value = split(line, ' ')[1];
  54.  
  55.             for (int i = 0; i < vec.size(); i++) {
  56.                 if (vec[i].first == index) {
  57.                     vec[i].second = value;
  58.                     break;
  59.                 }
  60.             }
  61.  
  62.             sort(vec.begin(), vec.end(), [](pair<int, int> a,
  63.                                             pair<int, int> b) {
  64.                 return a.second < b.second;
  65.             });
  66.         }
  67.     }
  68.  
  69.     fin.close();
  70.     fout.close();
  71.  
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement