-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvention2.txt
68 lines (53 loc) · 2 KB
/
convention2.txt
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
import java.io.*;
import java.util.*;
public class convention2 {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("convention2.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("convention2.out")),true);
int cowsnum = Integer.parseInt(f.readLine());
int[][] cows = new int[cowsnum][3];
for(int i=0; i<cowsnum; i++){
StringTokenizer st = new StringTokenizer(f.readLine());
cows[i][0]=cowsnum-i;
cows[i][1]=Integer.parseInt(st.nextToken());
cows[i][2]=Integer.parseInt(st.nextToken());
}
PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] ints, int[] t1) {
return -1*Integer.compare(ints[0],t1[0]);
}
});
Arrays.sort(cows, new Comparator<int[]>() {
@Override
public int compare(int[] ints, int[] t1) {
return Integer.compare(ints[1],t1[1]);
}
});
int index=0;
int time = cows[0][1];
ArrayList<Integer> delay = new ArrayList<>();
while (index<cowsnum && time>=cows[index][1]){
queue.add(cows[index]);
index++;
}
while(queue.size()>0){
int[] curr = queue.poll();
delay.add(time - curr[1]);
time = time + curr[2];
while (index<cowsnum&&time>=cows[index][1]){
queue.add(cows[index]);
index++;
}
if(queue.size()==0&&index<cowsnum){
time=cows[index][1];
}
while (index<cowsnum&&time>=cows[index][1]){
queue.add(cows[index]);
index++;
}
}
out.println(Collections.max(delay));
out.close();
}
}