fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Navigate {
  4. public:
  5. int currentFloor;
  6. int sourceFloor;
  7. int destinationFloor;
  8. int timeStamp;
  9. Navigate () {
  10. currentFloor = 0;
  11. };
  12. Navigate ( int currentFloor, int intputSourceFloor, int inputDestinationFloor, int inputTimestamp) :
  13. currentFloor(currentFloor) ,sourceFloor(intputSourceFloor) , destinationFloor(inputDestinationFloor) ,timeStamp(inputTimestamp)
  14. {};
  15. };
  16.  
  17. class QueueSystem {
  18. public:
  19. set<Navigate> liftQueue;
  20. set<pair<int,int> > nextInstuctionFloor;
  21. int getCurrentFloor () {
  22. if( !liftQueue.empty() ) {
  23. auto firstInstruction = *liftQueue.begin();
  24. return firstInstruction.currentFloor;
  25. }
  26. return 0;
  27. }
  28. int getLatestTimeStamp () {
  29. if( !liftQueue.empty() ) {
  30. auto firstInstruction = *liftQueue.rbegin();
  31. return firstInstruction.timeStamp;
  32. }
  33. return 0;
  34. }
  35. } queueSystem;
  36.  
  37. int getNextFloor() {
  38. if ( !queueSystem.liftQueue.empty() ) {
  39. auto nextDirectionInfo = *queueSystem.liftQueue.begin();
  40. queueSystem.liftQueue.erase(nextDirectionInfo);
  41. auto next = *queueSystem.nextInstuctionFloor.begin();
  42. // If movement direction is same
  43. if ( !( (next.first < next.second ) ^ (nextDirectionInfo.sourceFloor < nextDirectionInfo.destinationFloor) ) ) {
  44. // If current nextDirectionInfo [ 2 -> 4 ] ( source & destinationFloor)
  45. // But there is some movement [3 - > 5 ] then 3 is return after reaching 2
  46. if ( next.first > nextDirectionInfo.sourceFloor && nextDirectionInfo.destinationFloor < next.second ){
  47. queueSystem.nextInstuctionFloor.erase(next);
  48. return next.first;
  49. }
  50. }
  51. return nextDirectionInfo.destinationFloor;
  52. }
  53. return 0;
  54. }
  55.  
  56. bool operator < ( const Navigate &firstNavigation , const Navigate &secondNavigation) {
  57. return firstNavigation.timeStamp < secondNavigation.timeStamp;
  58. }
  59.  
  60. void processInput(int from_floor, int to_floor, int timestamp) {
  61. if ( from_floor != to_floor ){
  62. return;
  63. }
  64. int currentFloor = 0, timeStamp = 0;
  65. currentFloor = queueSystem.getCurrentFloor();
  66. timeStamp = queueSystem.getLatestTimeStamp();
  67. Navigate newUserInput(currentFloor,from_floor,to_floor,timestamp);
  68. queueSystem.liftQueue.insert(newUserInput);
  69. queueSystem.nextInstuctionFloor.insert({from_floor,to_floor});
  70. }
  71.  
  72. int main() {
  73. return 0;
  74. }
Success #stdin #stdout 0s 4404KB
stdin
Standard input is empty
stdout
Standard output is empty