Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <map>
  5. #include <vector>
  6. #include <unordered_map>
  7. #include <numeric>
  8. #include <random>
  9. #include <set>
  10.  
  11. //#define FAST_ALLOCATOR_MEMORY 1e8
  12. #include "optimization.h"
  13.  
  14. using namespace std;
  15.  
  16. struct S {
  17.     long long sum;
  18.     string city;
  19.  
  20.     S() {}
  21.     S(long long _sum, string _city) : sum(_sum), city(_city) {}
  22.  
  23.     bool operator < (const S& s) const {
  24.         if (sum == s.sum)
  25.             return city < s.city;
  26.         return sum > s.sum;
  27.     }
  28. };
  29.  
  30. signed main(void) {
  31.     #ifdef LOCAL
  32.         freopen("a.in", "r", stdin);
  33.         freopen("a.out", "w", stdout);
  34.     #endif
  35.     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  36.     int n = readInt();
  37.     char* ss = (char*)malloc(50 * sizeof(char));
  38.     string name, city;
  39.     unordered_map<string, long long> cash(5 * n), sumCity(5 * n);
  40.     unordered_map<string, string> cur(5 * n);
  41.     map<string, int> ans;
  42.     long long c;
  43.     for (int i = 0; i < n; ++i) {
  44.         readWord(ss);
  45.         name = string(ss);
  46.         readWord(ss);
  47.         city = string(ss);
  48.         c = readInt<long long>();
  49.         cash[name] = c;
  50.         cur[name] = city;
  51.         sumCity[city] += c;
  52.     }  
  53.     int days = readInt(), q = readInt();
  54.     int curDay, lastDay = 0;
  55.     set<S> s;
  56.     for (auto it = sumCity.begin(); it != sumCity.end(); ++it) {
  57.         s.insert({it->second, it->first});
  58.     }
  59.     for (int i = 0; i < q; ++i) {
  60.         curDay = readInt();
  61.        
  62.         if (curDay != lastDay && ((int)s.size() == 1 || (*s.begin()).sum > (*(++s.begin())).sum)) {
  63.             ans[(*s.begin()).city] += curDay - lastDay;
  64.         }
  65.         lastDay = curDay;
  66.        
  67.         readWord(ss);
  68.         name = string(ss);
  69.         readWord(ss);
  70.         city = string(ss);
  71.  
  72.         s.erase({sumCity[cur[name]], cur[name]});
  73.         sumCity[cur[name]] -= cash[name];
  74.         s.insert({sumCity[cur[name]], cur[name]});
  75.  
  76.         s.erase({sumCity[city], city});
  77.         sumCity[city] += cash[name];
  78.         s.insert({sumCity[city], city});
  79.  
  80.         cur[name] = city;
  81.     }
  82.     if ((int)s.size() == 1 || (*s.begin()).sum > (*(++s.begin())).sum) {
  83.         ans[(*s.begin()).city] += days - lastDay;
  84.     }
  85.     for (auto it = ans.begin(); it != ans.end(); ++it) {
  86.         cout << it->first << ' ' << it->second << endl;
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement