fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool shouldSendAlert(map<int, deque<int>>& mp, int timeLimit, int eventLimit, int eventId, int eventTs, int userId){
  5. deque<int>& dq = mp[userId];
  6. int firstTs = dq.front();
  7. cout << "EventInput:" << eventId << " " << eventTs << " " << userId << endl ;
  8. bool alertUser = false;
  9. while(eventTs - firstTs >= timeLimit && dq.size() > 0){
  10. cout << "Popped Events" << dq.front() << endl;
  11. dq.pop_front();
  12. firstTs = dq.front();
  13. }
  14. if(dq.size() < eventLimit){
  15. alertUser = true;
  16. cout << "Alerted Event" << eventTs << endl;
  17. }
  18. dq.push_back(eventTs);
  19. mp[userId] = dq;
  20. return alertUser;
  21.  
  22. }
  23.  
  24. int main(){
  25. int noOfTestCases;
  26. cin >> noOfTestCases;
  27. int timeLimit;
  28. cin >> timeLimit;
  29.  
  30. int eventLimit;
  31. cin >> eventLimit;
  32.  
  33. int i = 0 ;
  34. map<int, deque<int>> mp;
  35. while(i < noOfTestCases){
  36. int eventId, eventTs, userId;
  37. cin >> eventId >> eventTs >> userId;
  38. cout << shouldSendAlert(mp, timeLimit, eventLimit,eventId, eventTs, userId);
  39. cout << endl;
  40. i++;
  41. }
  42.  
  43. }
Success #stdin #stdout 0.01s 5284KB
stdin
10
3
1 10 1
2 14 1
3 17 1
4 20 1
8 25 1
5 10 2
6 14 2
7 18 2
9 21 2
10 22 2

stdout
EventInput:10 1 2
Alerted Event1
1
EventInput:14 1 3
Alerted Event1
1
EventInput:17 1 4
Alerted Event1
1
EventInput:20 1 8
Alerted Event1
1
EventInput:25 1 5
Alerted Event1
1
EventInput:10 2 6
Alerted Event2
1
EventInput:14 2 7
Alerted Event2
1
EventInput:18 2 9
Alerted Event2
1
EventInput:21 2 10
Alerted Event2
1
EventInput:22 2 10
0