-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorkstations.java
51 lines (46 loc) · 1.18 KB
/
Workstations.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
//Name: Nguyen Minh Hieu
//https://open.kattis.com/problems/workstations
import java.util.*;
public class Workstations {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Integer n = sc.nextInt();
Integer m = sc.nextInt();
sc.nextLine();
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
ArrayList<Integer[]> researchers = new ArrayList<Integer[]>();
for (Integer i = 0; i < n; i ++) {
Integer start = sc.nextInt();
Integer stay = sc.nextInt();
sc.nextLine();
Integer time[] = {start, start+stay-1}; //format (start, end)
researchers.add(time);
}
//sort researchers by starting time
Collections.sort(researchers, (a,b) -> a[0]-b[0]);
Integer saves = 0;
for (Integer[] researcher: researchers) {
if (pq.size() == 0) {
pq.add(researcher[1]);
continue;
}
while (pq.size() > 0) {
Integer w = pq.peek();
if (w >= researcher[0]) {
pq.add(researcher[1]);
break;
}
if (w < researcher[0] && researcher[0] <= w + m +1) {
pq.poll();
pq.add(researcher[1]);
saves++;
break;
}
if (researcher[0] > w + m + 1) {
pq.poll();
}
}
}
System.out.println(saves);
}
}