-
Notifications
You must be signed in to change notification settings - Fork 368
/
Highest_Response_Ratio_Next.java
138 lines (110 loc) · 4.44 KB
/
Highest_Response_Ratio_Next.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.util.*;
import java.util.LinkedList;
public class Highest_Response_Ratio_Next {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int pnum = scanner.nextInt();
ArrayList<Integer> at = new ArrayList<>();
ArrayList<Integer> bt = new ArrayList<>();
for (int i = 0; i < pnum; i++) {
at.add(scanner.nextInt());
}
for (int i = 0; i < pnum; i++) {
bt.add(scanner.nextInt());
}
ArrayList<Pair<Integer, Pair<Integer, Integer>>> v = new ArrayList<>();
Queue<Pair<Integer, Pair<Integer, Integer>>> readyQueue = new LinkedList<>();
Queue<Pair<Integer, Pair<Integer, Integer>>> processQueue = new LinkedList<>();
Map<Integer, Integer> busin = new HashMap<>();
Map<Integer, Integer> tat = new HashMap<>();
Map<Integer, Integer> wt = new HashMap<>();
//ind->in bus
for (int i = 0; i < pnum; i++) {
v.add(new Pair<>(i, new Pair<>(at.get(i), bt.get(i))));
}
int ct = 0;
int listptr = 0;
Map<Integer, Integer> completion = new HashMap<>();
while (listptr < pnum || !processQueue.isEmpty() || !readyQueue.isEmpty()) {
while (listptr < pnum) {
Pair<Integer, Pair<Integer, Integer>> nextProcess = v.get(listptr);
int nxtArrivalTime = nextProcess.second.first;
boolean newProcessArrival = (nxtArrivalTime == ct);
if (newProcessArrival) {
readyQueue.add(nextProcess);
listptr++;
} else {
break;
}
}
if (!processQueue.isEmpty()) {
Pair<Integer, Pair<Integer, Integer>> execProcess = processQueue.peek();
int btValue = execProcess.second.second;
int pid = execProcess.first;
if (busin.get(pid) + btValue == ct) {
processQueue.poll();
completion.put(pid, ct);
}
}
if (processQueue.isEmpty()) {
float mr = -1;
Pair<Integer, Pair<Integer, Integer>> evalProcess = null;
Queue<Pair<Integer, Pair<Integer, Integer>>> tmpQueue = new LinkedList<>();
while (!readyQueue.isEmpty()) {
Pair<Integer, Pair<Integer, Integer>> it = readyQueue.poll();
tmpQueue.add(it);
int atime = it.second.first;
int btime = it.second.second;
int wtime = ct - atime;
float res = (wtime + btime) / (float) btime;
if (res > mr) {
mr = res;
evalProcess = it;
}
}
while (!tmpQueue.isEmpty()) {
Pair<Integer, Pair<Integer, Integer>> it = tmpQueue.poll();
if (it.equals(evalProcess)) continue;
readyQueue.add(it);
}
if (mr > -1) {
int pid = evalProcess.first;
processQueue.add(evalProcess);
busin.put(pid, ct);
}
}
ct++;
}
System.out.println("The Process ids: mapped with the completion times are:");
for (Map.Entry<Integer, Integer> entry : completion.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
float avg_waiting, avg_tat;
for(int i = 0;i<pnum;i++) {
// tat[i] = completion[i] - at[i];
tat.put(i, completion.get(i) - at.get(i));
}
for(int i = 0;i<pnum;i++) {
// wt[i] = tat[i] - bt[i];
wt.put(i, tat.get(i) - bt.get(i));
}
int waitSum = 0,tatSum = 0;
for(int i = 0;i<pnum;i++) {
waitSum += wt.get(i);
tatSum += tat.get(i);
}
avg_tat = tatSum/((float)pnum);
avg_waiting = waitSum/((float)pnum);
System.out.println("Average TAT is " + avg_tat + " units");
System.out.println("Average WT is " + avg_waiting + " units");
scanner.close();
}
static class Pair<T1, T2> {
T1 first;
T2 second;
public Pair(T1 first, T2 second) {
this.first = first;
this.second = second;
}
}
}