-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvention2.java
More file actions
101 lines (99 loc) · 2.38 KB
/
Convention2.java
File metadata and controls
101 lines (99 loc) · 2.38 KB
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
import java.io.*;
import java.util.*;
public class Convention2 {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("convention2.in"));
int num = Integer.parseInt(br.readLine());
Comparator<Cow> compareByArrival = new Comparator<Cow>()
{
//negative if this object is less
//zero if equal
//positive if greater
public int compare(Cow c1, Cow c2)
{
return c1.getArrivalTime() - c2.getArrivalTime();
}
};
PriorityQueue<Cow> cowTimes = new PriorityQueue<Cow>(num, compareByArrival);
String[] temp;
for (int i = 0; i < num; i++)
{
temp = br.readLine().split(" ");
cowTimes.add(new Convention2().new Cow(i, Integer.parseInt(temp[0]), Integer.parseInt(temp[1])));
}
Comparator<Cow> compareByPriority = new Comparator<Cow>()
{
public int compare(Cow c1, Cow c2)
{
return c1.getPriority() - c2.getPriority();
}
};
PriorityQueue<Cow> waitingQueue = new PriorityQueue<Cow>(1, compareByPriority);
int timestamp = 0;
Cow outCow;
int max = 0;
while (!cowTimes.isEmpty() || !waitingQueue.isEmpty())
{
while (cowTimes.peek()!=null)
{
if (cowTimes.peek().getArrivalTime() <= timestamp)
{
waitingQueue.add(cowTimes.poll());
}
else
break;
}
if (!waitingQueue.isEmpty())
{
outCow = waitingQueue.poll();
if (timestamp - outCow.getArrivalTime() > max)
max = timestamp - outCow.getArrivalTime();
timestamp = timestamp + outCow.getDuration();
}
else
{
if (cowTimes.isEmpty())
break;
timestamp = cowTimes.peek().getArrivalTime();
}
//System.out.println(cowTimes);
//System.out.println(waitingQueue);
//System.out.println(timestamp);
}
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("convention2.out")));
pw.println(max);
pw.close();
//System.out.println(max);
br.close();
}
public class Cow
{
private int priority;
private int time_arrive;
private int duration;
public Cow(int p, int a, int t)
{
priority = p;
time_arrive = a;
duration = t;
}
public int getPriority()
{
return priority;
}
public int getArrivalTime()
{
return time_arrive;
}
public int getDuration()
{
return duration;
}
//for debugging
public String toString()
{
return "\nPriority: "+ priority + "\nArrival: " + time_arrive + "\nDuration: " + duration;
}
}
}