-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFCFS.cpp
69 lines (61 loc) · 1.6 KB
/
FCFS.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef list<int> li;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<bool> vb;
typedef pair<int,int> pii;
typedef vector<pair<int,int> > vpii;
typedef pair<double,double> pdd;
typedef vector<pair<double,double> > vpdd;
#define rep(i,n) for(int i=0;i<n;i++)
#define repi(i,k,n) for(int i=k;i<n;i++)
#define mp make_pair
#define pb push_back
#define all(a) a.begin(),a.end()
#define imax numeric_limits<int>::max()
#define imin numeric_limits<int>::min()
#define dmax numeric_limits<double>::max()
#define dmin numeric_limits<double>::min()
#define MAX 100
int n;
struct job {
int pid,at,bt,tt,ct,wt;
} jobs[MAX];
bool cmp (const struct job &a, const struct job &b) {
return a.at < b.at;
}
bool cmp2 (const struct job &a, const struct job &b) {
return a.pid < b.pid;
}
void fcfs () {
sort(jobs,jobs+n,cmp);
int temp = 0;
rep(i,n) {
temp += jobs[i].bt;
jobs[i].ct = temp;
}
int tot_wait = 0;
rep(i,n) {
jobs[i].tt = jobs[i].ct - jobs[i].at;
jobs[i].wt = jobs[i].tt - jobs[i].bt;
tot_wait += jobs[i].wt;
}
sort(jobs,jobs+n,cmp2);
// cout.setf(ios::fixed);
cout << " ID AT BT CT TT WT" << endl;
rep(i,n) {
// cout.width(4);
cout << jobs[i].pid << " " << jobs[i].at << " " << jobs[i].bt << " " << jobs[i].ct << " " << jobs[i].tt << " " << jobs[i].wt << endl;
}
cout << endl;
cout << "Average Wait time : " << double(tot_wait)/n;
}
int main() {
cin >> n;
rep(i,n)
cin >> jobs[i].pid >> jobs[i].at >> jobs[i].bt;
fcfs();
}