Advertisement
pb_jiang

leetcode 2102

Jul 13th, 2022
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. using pis = pair<int, string>;
  2. bool cmp1(const pis &a, const pis &b)
  3. {
  4.     if (a.first != b.first)
  5.         return a.first < b.first;
  6.     return a.second > b.second;
  7. }
  8. bool cmp2(const pis &a, const pis &b)
  9. {
  10.     if (a.first != b.first)
  11.         return a.first > b.first;
  12.     return a.second < b.second;
  13. }
  14. class SORTracker
  15. {
  16.     priority_queue<pis, vector<pis>, decltype(&cmp1)> pq1;
  17.     priority_queue<pis, vector<pis>, decltype(&cmp2)> pq2;
  18.     int curr;
  19.  
  20.   public:
  21.     SORTracker()
  22.     {
  23.         printf("construction\n");
  24.         curr = 0;
  25.     }
  26.  
  27.     void add(string name, int score)
  28.     {
  29.         printf("add\n");
  30.         auto ele = pis{score, name};
  31.         if (pq2.empty()) {
  32.             printf("pq1 push\n");
  33.             pq1.push(ele);
  34.             return;
  35.         }
  36.         printf("cmp2\n");
  37.         if (cmp2(ele, pq2.top())) {
  38.             pq1.push(ele);
  39.         } else {
  40.             pq2.push(ele);
  41.         }
  42.     }
  43.  
  44.     string get()
  45.     {
  46.         printf("get pq2.size():%lu\n", pq2.size());
  47.         /*
  48.         while (pq2.size() > curr + 1) {
  49.             auto ele = pq2.top();
  50.             pq2.pop();
  51.             pq1.push(ele);
  52.         }
  53.         while (pq2.size() < curr + 1) {
  54.             auto ele = pq1.top();
  55.             pq1.pop();
  56.             pq2.push(ele);
  57.         }
  58.         ++curr;
  59.         auto h = pq2.top();
  60.         return h.second;
  61.         */
  62.         return "";
  63.     }
  64. };
  65.  
  66. /**
  67.  * Your SORTracker object will be instantiated and called as such:
  68.  * SORTracker* obj = new SORTracker();
  69.  * obj->add(name,score);
  70.  * string param_2 = obj->get();
  71.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement