akela43

очередь с приоритетами параллельно stepik

May 28th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. #include <sstream>
  5. #include <iterator>
  6. using namespace std;
  7. using Ttime = long long int;
  8. using T = std::pair<Ttime, int>;
  9. template<class InputIt>
  10. void pq(InputIt is, ostream &os) {
  11.     std::priority_queue <T, vector <T>, greater<T>> q;
  12.     int n, m;
  13.     n = *is++;
  14.     m = *is;
  15.     T value{0, 0};
  16.     Ttime t;
  17.     for (auto i = 0; i < m; ++i) {
  18.         t = *++is;
  19.         os << value.second << " " << value.first << "\n";
  20.         if (t > 0) {
  21.             q.push({t + value.first, value.second});
  22.             if (q.size() >= n) {
  23.                 value = q.top();
  24.                 q.pop();
  25.             }
  26.             else
  27.                 value.second++;
  28.         }
  29.     }
  30. }
  31.  
  32. int main() {
  33.     std::vector<int> v1 = {16, 12, 4, 5, 2, 0, 1, 0, 7, 2, 6, 8, 0, 0};
  34.     std::vector<int> v2 = {1, 12, 2, 0, 4, 3, 9, 8, 4, 9, 0, 4, 3, 2};
  35.     //pq(v1.begin(), cout);
  36.    //pq(v2.begin(), cout);
  37.     pq(istream_iterator<int> (cin), cout);
  38. }
Add Comment
Please, Sign In to add comment