Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. set<pair<int,int> >hi,;
  2. set<pair<int,int>,greater<pair<int,int> > >lo;
  3. void balance()
  4. {
  5. while(hi.size() > lo.size())
  6. {
  7. lo.insert(hi.begin());
  8. hi.erase(hi.begin());
  9. }
  10. while(lo.size()-1 > hi.size() )
  11. {
  12. hi.insert(lo.begin());
  13. lo.erase(lo.begin());
  14. }
  15. }
  16. int activityNotifications(vector<int> expenditure, int d) {
  17.  
  18.  
  19. for(int i=0;i<expenditure.size();i++)
  20. {
  21. lo.insert( make_pair(expenditure[i],i) );
  22. balance();
  23.  
  24. if(i>=d)
  25. {
  26. auto it = lo.lower_bound( make_pair(expenditure[i-d],i-d) );
  27. if(it!=lo.end()) lo.erase(it);
  28. else {
  29. auto it = hi.lower_bound( make_pair(expenditure[i-d],i-d) );
  30. if(it!=hi.end()) hi.erase(it);
  31. }
  32. }
  33. balance();
  34. }
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement