fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class T{
  5. public:
  6. string process;
  7. int burstTime;
  8. int priority;
  9. int arrival;
  10.  
  11. int wt = 0;
  12.  
  13. T(){};
  14.  
  15. T(string pro, int bas, int pri, int ari){
  16. this->process = pro;
  17. this->burstTime = bas;
  18. this->priority = pri;
  19. this->arrival = ari;
  20. }
  21. };
  22.  
  23. void Merge(vector<T>&A, int L, int M, int R){
  24. int n1 = M - L + 1;
  25. int n2 = R - M;
  26. vector<T>X(n1), Y(n2);
  27. for(int i = 0; i < n1; i++){
  28. X[i] = A[L+i];
  29. }
  30. for(int i = 0; i < n2; i++){
  31. Y[i] = A[M+1+i];
  32. }
  33.  
  34. int i = 0, j = 0, k = L;
  35. while(i < n1 and j < n2){
  36. if(X[i].arrival == Y[j].arrival){
  37. if(X[i].priority < Y[j].priority) A[k++] = X[i++];
  38. else A[k++] = Y[j++];
  39. }
  40. else if(X[i].arrival < Y[j].arrival) A[k++] = X[i++];
  41. else A[k++] = Y[j++];
  42. }
  43.  
  44. while(i < n1) A[k++] = X[i++];
  45. while(j < n2) A[k++] = Y[j++];
  46. }
  47.  
  48. void mergeSort(vector<T>&A, int L, int R){
  49. if(L < R){
  50. int M = (L + R) / 2;
  51. mergeSort(A, L, M);
  52. mergeSort(A, M+1, R);
  53. Merge(A, L, M, R);
  54. }
  55. }
  56.  
  57. vector<string> DoWork(vector<T>t, int n){
  58. vector<T>demo;
  59. vector<string>v;
  60. for(int i = 0; i < n; i++){
  61. demo.push_back(t[i]);
  62.  
  63. sort(demo.begin(), demo.end(), [](T &a, T &b) {
  64. return a.priority < b.priority;
  65. });
  66.  
  67. demo[0].burstTime--;
  68. v.push_back(demo[0].process);
  69. if(demo[0].burstTime == 0) demo.erase(demo.begin() + 0);
  70. }
  71.  
  72. while(!demo.empty()){
  73. demo[0].burstTime--;
  74. v.push_back(demo[0].process);
  75. if(demo[0].burstTime == 0) demo.erase(demo.begin() + 0);
  76. }
  77. return v;
  78. }
  79.  
  80. void calculateWitingTime(vector<T>&A, vector<string>v){
  81. int n = A.size(), l = v.size();
  82. for(int i = 0; i < n; i++){
  83. int ct;
  84. for(int j = l-1; j >= 0; j--){
  85. if(v[j] == A[i].process){
  86. ct = j+1; break;
  87. }
  88. }
  89. A[i].wt = ct - A[i].arrival - A[i].burstTime;
  90. }
  91. }
  92.  
  93. void showGanttChart(vector<string>v){
  94. int n = v.size();
  95. cout << "Gantt Chart : " << endl;
  96. for(auto u : v){
  97. cout << u << " ";
  98. }
  99. cout << endl << endl;
  100. }
  101.  
  102. void showWaitingTime(vector<T>A, int n){
  103. float total = 0.0;
  104. cout << "Waiting Times : " << endl;
  105. for(int i = 0; i < n; i++){
  106. cout << A[i].process << " ==> " << A[i].wt << endl;
  107. total += A[i].wt;
  108. }
  109. cout << endl;
  110. cout << "Average Waiting Time : " << fixed << setprecision(2) << total / n << endl;
  111. }
  112.  
  113. int main(){
  114. int n; cin >> n;
  115.  
  116. vector<T>t(n);
  117. for(int i = 0; i < n; i++){
  118. string pro; cin >> pro;
  119. int bas, pri, ari; cin >> bas >> pri >> ari;
  120.  
  121. t[i] = T(pro, bas, pri, ari);
  122. }
  123.  
  124. mergeSort(t, 0, n-1);
  125.  
  126. vector<string>ans = DoWork(t, n);
  127. showGanttChart(ans);
  128.  
  129. calculateWitingTime(t, ans);
  130. showWaitingTime(t, n);
  131.  
  132. return 0;
  133. }
Success #stdin #stdout 0s 5272KB
stdin
6
p1 10 2 1
p2 1 4 0
p3 2 1 3
p4 1 3 2
p5 5 5 3
p6 4 6 5
stdout
Gantt Chart : 
p2 p1 p1 p3 p3 p1 p1 p1 p1 p1 p1 p1 p1 p4 p5 p5 p5 p5 p5 p6 p6 p6 p6 

Waiting Times : 
p2 ==> 0
p1 ==> 2
p4 ==> 11
p3 ==> 0
p5 ==> 11
p6 ==> 14

Average Waiting Time : 6.33