fork download
  1. //Discrete Event Simulation
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<set>
  5. #include<utility>
  6. using namespace std;
  7. int main()
  8. {int t,p,q,m,n,k;
  9. int landed,takenoff,waitingtoland,waitingtotakeoff;
  10. scanf("%d",&t);
  11. while(t--)
  12. {scanf("%d%d%d%d%d",&p,&q,&m,&n,&k);//m is inter-landing time n-is inter-takeoff time
  13. //at same time t priority is given to landing
  14. landed=takenoff=0;
  15. waitingtoland=p;
  16. waitingtotakeoff=q;
  17. multiset< pair<int,int> >heap;//if int is 0 then the event is landing
  18. for(int i=1;i<=waitingtoland;i++)
  19. heap.insert(make_pair(i*m,0));
  20. for(int i=1;i<=waitingtotakeoff;i++)
  21. heap.insert(make_pair(i*n,1));
  22. while(!heap.empty())
  23. {multiset<pair<int,int> >::iterator curr;
  24. curr=heap.begin();
  25. pair<int,int>e=*curr;
  26. if(e.first>k)
  27. break;
  28. /*for(;curr!=heap.end();curr++)
  29.   printf("time=%d event(0 for landing)=%d\n",curr->first,curr->second);
  30.   printf("%d %d %d %d\n",landed,takenoff,waitingtoland,waitingtotakeoff);
  31.  
  32.   printf("-------\n");
  33.   curr=heap.begin();*/
  34. heap.erase(curr);
  35. if(e.second==0)//landing event
  36. {waitingtoland--;
  37. landed++;
  38. heap.insert(make_pair(e.first+n,1));//takeoff event created
  39. waitingtotakeoff++;
  40. }
  41. else//takeoff event
  42. {waitingtotakeoff--;
  43. takenoff++;
  44. heap.insert(make_pair(e.first+m,0));//landing event created
  45. waitingtoland++;
  46. }
  47.  
  48. }
  49. printf("%d %d %d %d\n",landed,takenoff,waitingtoland,waitingtotakeoff);
  50. }
  51. return 0;
  52. }
Success #stdin #stdout 0s 3476KB
stdin
3
2 3 5 2 15
3 2 3 5 10
4 6 6 2 18
stdout
7 9 4 1
4 3 2 3
12 15 7 3