chillurbrain

Доп. из 13. Миллиардеры.

May 26th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <set>
  4. #include <map>
  5. #include <unordered_map>
  6. #include <iostream>
  7. #include <functional>
  8. #include <algorithm>
  9.  
  10. const int maxn = 10000, maxm = 50000, maxk = 50000;
  11.  
  12. struct City
  13. {
  14.     long long money;
  15.     int days;
  16.     std::string name;
  17. } cities[maxn + maxk];
  18.  
  19. struct Person
  20. {
  21.     std::string name;
  22.     long long money;
  23.     City* location;
  24. } persons[maxn];
  25.  
  26. std::unordered_map<std::string, Person*> personMap;
  27. std::map<std::string, City*> cityMap;
  28. std::set<std::pair<long long, City*>, std::greater<>> scoreBoard;
  29.  
  30. int main()
  31. {
  32.     std::ios::sync_with_stdio(false);
  33.     int n, m, k, c = 0;
  34.     std::cin >> n;
  35.     for (int i = 0; i < n; i++)
  36.     {
  37.         std::string name, location; long long money;
  38.         std::cin >> name >> location >> money;
  39.         personMap[name] = &persons[i];
  40.         if (!cityMap[location])
  41.             cityMap[location] = &cities[c++];
  42.         persons[i].location = cityMap[location];
  43.         persons[i].money = money;
  44.         cityMap[location]->name = location;
  45.         cityMap[location]->money += money;
  46.     }
  47.     for (auto c : cityMap)
  48.         scoreBoard.insert({ c.second->money, c.second });
  49.     std::cin >> m >> k;
  50.     int prevday = 0, day;
  51.     std::string name, place;
  52.     while (true)
  53.     {
  54.         bool finished = (!(std::cin >> day >> name >> place));
  55.         if (finished)
  56.             day = m;
  57.         if (day != prevday)
  58.         {
  59.             auto it2 = scoreBoard.begin();
  60.             auto it = it2++;
  61.             if (it2 == scoreBoard.end() || it->first > it2->first)
  62.                 it->second->days += day - prevday;
  63.         }
  64.         if (finished)
  65.             break;
  66.         if (!cityMap[place])
  67.         {
  68.             cityMap[place] = &cities[c++];
  69.             cityMap[place]->name = place;
  70.         }
  71.         auto nextplace = cityMap[place];
  72.         auto person = personMap[name];
  73.         auto prevplace = person->location;
  74.         auto prevmoney = prevplace->money;
  75.         scoreBoard.erase({ prevmoney, prevplace });
  76.         prevplace->money -= person->money;
  77.         scoreBoard.insert({ prevplace->money, prevplace });
  78.         scoreBoard.erase({ nextplace->money, nextplace });
  79.         nextplace->money += person->money;
  80.         scoreBoard.insert({ nextplace->money, nextplace });
  81.         person->location = nextplace;
  82.         prevday = day;
  83.     }
  84.     std::vector<std::pair<std::string, int>> output;
  85.     for (auto& r : scoreBoard)
  86.     if (r.second->days)
  87.         output.push_back(std::make_pair(r.second->name, r.second->days));
  88.     std::sort(output.begin(), output.end());
  89.     for (auto& o : output)
  90.         std::cout << o.first << " " << o.second << "\n";
  91. }
Advertisement
Add Comment
Please, Sign In to add comment