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. auto& dq = mp[userId]; // Reference, no copy
  6.  
  7. // 1. Remove timestamps outside the window
  8. while (!dq.empty() && eventTs - dq.front() > timeLimit) {
  9. dq.pop_front();
  10. }
  11.  
  12. // 2. Decide whether to send alert
  13. bool send = dq.size() < eventLimit;
  14.  
  15. // 3. Always push current timestamp
  16. dq.push_back(eventTs);
  17.  
  18. return send;
  19.  
  20. }
  21.  
  22. int main(){
  23. int noOfTestCases;
  24. cin >> noOfTestCases;
  25. int timeLimit;
  26. cin >> timeLimit;
  27.  
  28. int eventLimit;
  29. cin >> eventLimit;
  30.  
  31. int i = 0 ;
  32. map<int, deque<int>> mp;
  33. while(i < noOfTestCases){
  34. int eventId, eventTs, userId;
  35. cin >> eventId >> eventTs >> userId;
  36. cout << shouldSendAlert(mp, timeLimit, eventLimit,eventId, eventTs, userId);
  37. cout << endl;
  38. i++;
  39. }
  40.  
  41. }
Success #stdin #stdout 0.01s 5316KB
stdin
10
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
1
1
1
0
1
1
1
1
1
0