-
Notifications
You must be signed in to change notification settings - Fork 0
/
Downtime.java
85 lines (78 loc) · 1.89 KB
/
Downtime.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
import java.util.*;
import java.io.*;
/**
* Kattis problem downtime
*
* Difficulty: 4.0
* Solved: 2017-11-24
*/
public class Downtime {
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
int nRequests=sc.nextInt();
int slotsPerServer=sc.nextInt();
int nSlots = slotsPerServer;
PriorityQueue<Event> q=new PriorityQueue<>(nRequests+10,new EventComparator());
Set<Integer> free=new HashSet<>(nSlots*2);
for(int i=0;i<nSlots;i++){
free.add(i);
}
for(int i=0;i<nRequests;i++){
int startTime = sc.nextInt();
q.add(Event.start(startTime, startTime+1000));
}
while(!q.isEmpty()){
Event e=q.poll();
switch(e.type){
case Event.START: {
if(!free.isEmpty()){
int slot=free.iterator().next();
q.add(Event.end(e.end,slot));
free.remove(slot);
//System.out.printf("added in slot %d\n",slot);
} else {
q.add(Event.end(e.end,nSlots));
nSlots++;
//System.out.printf("increased to %d slots\n",nSlots);
}
}
break;
case Event.END: {
free.add(e.slot);
}
break;
}
}
System.out.print(nSlots/slotsPerServer+(nSlots%slotsPerServer==0?0:1));
}
public static class Event {
final int time;
final int type;
final int slot;
final int end;
static final int START = 1;
static final int END = 0;
private Event(int time, int type, int slot, int end){
this.time=time;
this.type=type;
this.end=end;
this.slot=slot;
}
static Event start(int time, int end){
return new Event(time,START,-1,end);
}
static Event end(int time, int slot){
return new Event(time,END,slot,-1);
}
}
public static class EventComparator implements Comparator<Event> {
public int compare(Event a, Event b){
int r1 = Integer.compare(a.time,b.time);
if(r1==0){
return Integer.compare(a.type,b.type);
} else {
return r1;
}
}
}
}