-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallestJob.cpp
80 lines (76 loc) · 1.79 KB
/
SmallestJob.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
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<bits/stdc++.h>
using namespace std;
#define pp pair<int,int>
class Compare
{
public:
bool operator() (pair<int,int>& a, pair<int,int>& b)
{
if(a.first>b.first){
return true;
}else{
return false;
}
}
};
int main(){
int n;
cout<<"Enter the number of process"<<endl;
cin>>n;
int ct[n];
int bt[n];
pp time[n];
int at[n];
cout<<"Enter the arrival time"<<endl;
for(int i=0;i<n;i++){
cin>>time[i].first;
at[i]=time[i].first;
time[i].second=i;
}
sort(time,time +n);
cout<<"Enter the bust time"<<endl;
for(int i=0;i<n;i++){
cin>>bt[i];
}
priority_queue<pp,vector<pp> ,Compare> pq;
int visited[n]={0};
int count=0;
for(int i=0;i<n;i++){
if(count<time[i].first){
count=time[i].first;
}
int j=i;
while(j<n and time[j].first<=count){
if(visited[j]==0){
visited[j]=1;
pp curr =make_pair(bt[time[j].second],time[j].second);
pq.push(curr);
}
j++;
}
pp curr=pq.top();
count+=curr.first;
ct[curr.second]=count;
pq.pop();
}
// for(int i=0;i<n;i++){
// cout<<time[i].first<<" "<<time[i].second<<" "<<bt[time[i].second]<<endl;
// }
int tat[n];
for(int i=0;i<n;i++){
tat[i]=ct[i]-at[i];
}
cout<<"Completion time of earch process is as follows"<<endl;
for(int i=0;i<n;i++){
cout<<ct[i]<<" ";
}
cout<<endl;
double avgTat=0;
cout<<"Turnaround time of earch process is as follows"<<endl;
for(int i=0;i<n;i++){
cout<<tat[i]<<" ";
avgTat+=tat[i];
}
cout<<endl;
cout<<"Average tat is "<<avgTat/n<<endl;
}