-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcfs_with_arrival.cpp
More file actions
53 lines (41 loc) · 1.03 KB
/
fcfs_with_arrival.cpp
File metadata and controls
53 lines (41 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
struct Process {
int id, at, bt, wt, tat;
};
void printSequence(const vector<int>& seq) {
cout << "Job Sequence: ";
for (int i = 0; i < seq.size(); i++) {
cout << "P" << seq[i];
if (i != seq.size()-1) cout << " -> ";
}
cout << "\n\n";
}
int main() {
int n;
cin >> n;
vector<Process> p(n);
vector<int> seq;
for (int i = 0; i < n; i++) {
p[i].id = i+1;
cin >> p[i].at >> p[i].bt;
}
sort(p.begin(), p.end(), [](auto &a, auto &b){
return a.at < b.at;
});
for (auto &x : p)
seq.push_back(x.id);
printSequence(seq);
int time = 0;
for (int i = 0; i < n; i++) {
if (time < p[i].at)
time = p[i].at;
p[i].wt = time - p[i].at;
time += p[i].bt;
p[i].tat = p[i].wt + p[i].bt;
}
cout << "PID\tAT\tBT\tWT\tTAT\n";
for (auto &x : p)
cout << x.id << "\t" << x.at << "\t" << x.bt
<< "\t" << x.wt << "\t" << x.tat << "\n";
}