Advertisement
999ms

Untitled

Apr 23rd, 2021
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. собес 2, но нормальный код
  2. #include <bits/stdc++.h>
  3.  
  4.  
  5. class Data {
  6. public:
  7.   Data() {}
  8.  
  9.   void inc(const std::string& key) {
  10.     int counter = stringToCounter[key];
  11.    
  12.     auto it = counterToIterator.find(counter + 1);
  13.     auto prevIterator = counterToIterator.find(counter);
  14.    
  15.     std::list<std::pair<int, std::unordered_set<std::string>>>::iterator itNext, itPrev;
  16.    
  17.     if (it != counterToIterator.end()) {
  18.       itNext = it->second;
  19.     }
  20.    
  21.     if (prevIterator != counterToIterator.end()) {
  22.       itPrev = prevIterator->second;
  23.     }
  24.    
  25.    
  26.     if (counter > 0) {
  27.       removeStringFromBucket(key, prevIterator->second);    
  28.     }
  29.    
  30.     if (it == counterToIterator.end()) {
  31.       if (prevIterator == counterToIterator.end()) {
  32.         std::unordered_set<std::string> set;
  33.         itNext = data.emplace(data.begin(), counter + 1, set);
  34.         counterToIterator.emplace(counter + 1, itNext);
  35.       } else {
  36.         std::unordered_set<std::string> set;
  37.         itNext = data.emplace(std::next(prevIterator->second), counter + 1, set);
  38.         counterToIterator.emplace(counter + 1, itNext);
  39.       }
  40.     }
  41.    
  42.     stringToCounter[key]++;
  43.     addStringToBucket(key, itNext);
  44.   }
  45.  
  46.   void dec(const std::string& key) {
  47.     auto itCounter = stringToCounter.find(key);
  48.    
  49.     if (itCounter == stringToCounter.end()) {
  50.       return;
  51.     }
  52.    
  53.     int counter = itCounter->second;
  54.     itCounter->second--;
  55.    
  56.     auto it = counterToIterator.find(counter - 1);
  57.     auto prevIterator = counterToIterator.find(counter);
  58.    
  59.     std::list<std::pair<int, std::unordered_set<std::string>>>::iterator itNext, itPrev;
  60.    
  61.     if (it != counterToIterator.end()) {
  62.       itNext = it->second;
  63.     }
  64.    
  65.     if (prevIterator != counterToIterator.end()) {
  66.       itPrev = prevIterator->second;
  67.     }
  68.    
  69.     if (counter == 1) {
  70.       stringToCounter.erase(key);
  71.       removeStringFromBucket(key, itPrev);
  72.       return;
  73.     }
  74.    
  75.     if (it == counterToIterator.end()) {
  76.       std::unordered_set<std::string> set;
  77.       itNext = data.emplace(itPrev, counter - 1, set);
  78.       counterToIterator.emplace(counter - 1, itNext);
  79.     }
  80.    
  81.     removeStringFromBucket(key, itPrev);
  82.     addStringToBucket(key, itNext);
  83.   }
  84.  
  85.   std::string getMaxKey() const {
  86.     if (data.size() == 0) {
  87.       throw "No elements in Data";
  88.     }
  89.    
  90.     auto it = data.rbegin();
  91.    
  92.     return *((it->second).begin());
  93.   }
  94.  
  95.   std::string getMinKey() const {
  96.     if (data.size() == 0) {
  97.       throw "No elements in Data";
  98.     }
  99.    
  100.     auto it = data.begin();
  101.    
  102.     return *((it->second).begin());
  103.   }
  104.  
  105.   bool check(std::map<std::string, int> mp) {
  106.     for (auto& [s, cnt] : stringToCounter) {
  107.       if (mp[s] != cnt) {
  108.         std::cout << s << ' ' << cnt << ' ' << mp[s] << std::endl;
  109.         return false;
  110.       }
  111.       if (cnt == 0) return false;
  112.     }
  113.     return true;
  114.   }
  115.    
  116. private:
  117.  
  118.   void removeStringFromBucket(const std::string& key, std::list<std::pair<int, std::unordered_set<std::string>>>::iterator it) {
  119.     (it->second).erase(key);
  120.     if ((it->second).size() == 0) {
  121.       counterToIterator.erase(it->first);
  122.       data.erase(it);
  123.     }
  124.   }
  125.  
  126.   void addStringToBucket(const std::string& key, std::list<std::pair<int, std::unordered_set<std::string>>>::iterator it) {
  127.     (it->second).insert(key);
  128.   }
  129.  
  130.   std::list<std::pair<int, std::unordered_set<std::string>>> data;
  131.   std::unordered_map<std::string, int> stringToCounter;
  132.   std::unordered_map<int, std::list<std::pair<int, std::unordered_set<std::string>>>::iterator> counterToIterator;
  133.  
  134. };
  135.  
  136.  
  137. int main() {
  138.   Data data;
  139.   std::map<std::string, int> mp;
  140.  
  141.    
  142.   for (int i = 0; i < 100000000; i++) {
  143.     std::string cur = std::to_string(rand() % 1000);
  144.     int t = rand() & 1;
  145.     if (t) {
  146.       data.inc(cur);
  147.       mp[cur]++;
  148.     } else {
  149.       data.dec(cur);
  150.       if (mp[cur] > 0) mp[cur]--;
  151.     }
  152.    
  153.     if (i % 100000 == 0) {
  154.       std::cout << i << ' ' << "OK" << std::endl;
  155.     }
  156.   }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement