fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Process {
  5. private:
  6. int at;
  7. int bt;
  8. int ct;
  9. int tat;
  10. int wt;
  11. int pid;
  12.  
  13. public:
  14. int& operator[](string var)
  15. {
  16. if (var == "at")
  17. return at;
  18. if (var == "bt")
  19. return bt;
  20. if (var == "ct")
  21. return ct;
  22. if (var == "tat")
  23. return tat;
  24. if (var == "wt")
  25. return wt;
  26. return pid;
  27. }
  28.  
  29. void update_after_ct()
  30. {
  31. tat = ct - at;
  32. wt = tat - bt;
  33. }
  34.  
  35. void display()
  36. {
  37. printf("%d\t%d\t%d\t%d\t%d\t%d\n", pid, at, bt, ct,
  38. tat, wt);
  39. }
  40. };
  41.  
  42. float average(vector<Process> P, string var)
  43. {
  44. int total = 0;
  45. for (auto temp : P) {
  46. total += temp[var];
  47. }
  48. return (float)total / P.size();
  49. }
  50.  
  51. int main()
  52. {
  53. /*
  54.   Input description.
  55.   First line contains an integer n
  56.   the next n lines contains 2 space separated integers
  57.   containing values for arrival time and burst time for
  58.   example:
  59.   2
  60.   0 3
  61.   1 2
  62.   */
  63. int n;
  64. cin >> n;
  65. int counter = 0;
  66. vector<Process> P(n);
  67. for (Process& temp : P) {
  68. temp["id"] = counter++;
  69. cin >> temp["at"] >> temp["bt"];
  70. }
  71. sort(P.begin(), P.end(),
  72. [](Process first, Process second) {
  73. return first["at"] < second["at"];
  74. });
  75. printf("pid\tat\tbt\tct\ttat\twt\n");
  76. P[0]["ct"] = P[0]["at"] + P[0]["bt"];
  77. P[0].update_after_ct();
  78. P[0].display();
  79. for (int i = 1; i < P.size(); i++) {
  80. if (P[i]["at"] < P[i - 1]["ct"]) {
  81. P[i]["ct"] = P[i - 1]["ct"] + P[i]["bt"];
  82. }
  83. else {
  84. printf("curr['at'] : %d, prev['ct'] : %d\n",
  85. P[i]["at"], P[i - 1]["ct"]);
  86. P[i]["ct"] = P[i]["at"] + P[i]["bt"];
  87. }
  88. P[i].update_after_ct();
  89. P[i].display();
  90. }
  91.  
  92. printf("Average waiting time : %f\n", average(P, "wt"));
  93. return 0;
  94. }
Success #stdin #stdout 0s 5288KB
stdin
45
stdout
pid	at	bt	ct	tat	wt
34	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
23	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
24	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
25	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
26	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
27	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
28	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
29	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
30	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
31	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
32	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
33	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
22	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
35	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
36	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
37	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
38	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
39	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
40	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
41	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
42	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
43	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
44	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
11	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
1	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
2	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
3	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
4	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
5	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
6	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
7	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
8	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
9	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
10	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
0	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
12	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
13	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
14	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
15	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
16	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
17	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
18	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
19	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
20	0	0	0	0	0
curr['at'] : 0, prev['ct'] : 0
21	0	0	0	0	0
Average waiting time : 0.000000