Advertisement
artemgf

Мужья в магазине

Jun 15th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #pragma once
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #define _USE_MATH_DEFINES
  4. #include <iostream>
  5. #include <string>
  6. #include <map>
  7. #include <set>
  8. #include <algorithm>
  9. #include <vector>
  10. #include <stdio.h>
  11. #include <cmath>
  12. #include <math.h>
  13. #include <queue>
  14. #include <stack>
  15. #include <climits>
  16. #include <deque>
  17. #include <ctime>
  18. #include <iomanip>
  19. #include <bitset>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22.  
  23. using namespace std;
  24.  
  25. typedef long long ll;
  26. typedef unsigned long long ull;
  27. typedef unsigned int ui;
  28.  
  29. #define mh() make_heap()
  30. #define poph() pop_heap()
  31. #define pushh() push_heap()
  32. #define sor(n) n.begin(), n.end()
  33. #define rsor(n) n.rbegin(), n.rend()
  34. #define mp make_pair
  35. #define files freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout)
  36. #define p(T) pair<T,T>
  37. #define toch(x) cout.precision(x), cout.setf(ios::fixed)
  38. #define znac(l) abs(l)/l
  39. #define IOS ios::sync_with_stdio(false)
  40. #define IOSB cin.tie(0), cout.tie(0);
  41. const ll ok = ll(1e9 + 7);
  42.  
  43. int main()
  44. {
  45.     IOSB;
  46.     IOS;
  47. #ifdef TheCompiler
  48.     files;
  49. #endif
  50.     ll n;
  51.     map<string, ll>prod;
  52.     cin >> n;
  53.     for (int i = 1; i <= n; i++)
  54.     {
  55.         ll a;
  56.         string s;
  57.         cin >> a;
  58.         cin >> s >> s;
  59.         prod[s] = a;
  60.     }
  61.     cin >> n;
  62.     queue<pair<ll,string>>q;
  63.     for (int i = 1; i <= n; i++)
  64.     {
  65.         ll a;
  66.         string s;
  67.         cin >> a;
  68.         cin >> s >> s;
  69.         q.push({ a,s });
  70.     }
  71.     q.push({ -1,"" });
  72.  
  73.     deque<pair<ll,string>>now;
  74.     now.push_front(q.front());
  75.     q.pop();
  76.     ll time = 0;
  77.     while (!now.empty())
  78.     {
  79.         if (now.front().first == -1)
  80.         {
  81.             break;
  82.         }
  83.         time++;
  84.         if (prod.find(now.front().second) != prod.end() && prod[now.front().second] != 0)
  85.         {
  86.             if (prod[now.front().second] >= now.front().first)
  87.             {
  88.                 prod[now.front().second] -= now.front().first;
  89.                 now.pop_front();
  90.             }
  91.             else
  92.             {
  93.                 if (now.size() == 1)
  94.                 {
  95.                     pair<ll, string>nw = now.front();
  96.                     now.pop_front();
  97.                     nw.first = prod[nw.second];
  98.                     now.push_front(nw);
  99.                     now.push_front(q.front());
  100.                     q.pop();
  101.                 }
  102.                 else
  103.                 {
  104.                     pair<ll, string>nw = now.front();
  105.                     now.pop_front();
  106.                     nw.first = prod[nw.second];
  107.                     now.push_back(nw);
  108.                 }
  109.             }
  110.         }
  111.         else
  112.             now.pop_front();
  113.         if (now.empty())
  114.         {
  115.             now.push_front(q.front());
  116.             q.pop();
  117.         }
  118.     }
  119.     cout << time;
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement