This repository was archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvention2.java
More file actions
90 lines (86 loc) · 2.35 KB
/
convention2.java
File metadata and controls
90 lines (86 loc) · 2.35 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
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"));
StringTokenizer st;
int N = Integer.parseInt(f.readLine());
Map<cow,Integer> senority = new HashMap<cow,Integer>();
ArrayList<cow> time = new ArrayList<cow>();
for(int i = 0; i < N ; i ++) {
st = new StringTokenizer(f.readLine());
cow thecow = new cow(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
senority.put(thecow,i);
time.add(thecow);
thecow = null;
}
Collections.sort(time); // Looks a bit more stylish
//time.sort(null);
Comparator<cow> cowcompare = new Comparator<cow>() {
@Override
public int compare(cow arg0, cow arg1) {
// TODO Auto-generated method stub
return senority.get(arg0) - senority.get(arg1);
}
};
List<cow> theline = new ArrayList<cow>();
int c1; // Cache calculations
cow tc = new cow(-2,-2);
int mtime = -1,ctime,waittime;
ctime = 0;
cow c2;
int cows_eaten = 0;
int maxsenority;
//System.out.println(time);
while(cows_eaten < N || theline.size() > 0) {
if(theline.isEmpty()) {
tc = time.get(cows_eaten++);
ctime = tc.x;
}else {
tc = theline.remove(0);
waittime = ctime - tc.x;
if(waittime > mtime) {
mtime = waittime;
}
}
ctime = ctime + tc.y;
maxsenority = -1;
//cow nextcow = new cow(-1,-1); // Not guaranteed to have a conflicting time
int count = 0;
while(cows_eaten<N &&time.get(cows_eaten).x < ctime) {
theline.add(time.get(cows_eaten));
cows_eaten++;
count ++;
}
if(count>0) {theline.sort(cowcompare);}
}
//System.out.println(mtime);
PrintWriter pw = new PrintWriter(new FileWriter("convention2.out"));
pw.print(mtime);
pw.close();
}
}
class cow implements Comparable<cow>{
int x,y;
public cow(int i,int j) {
this.x = i;
this.y = j;
}
@Override
public int compareTo(cow arg0) {
// TODO Auto-generated method stub
return this.x - arg0.x;
}
@Override
public String toString() {
return "("+this.x+","+this.y+")";
}
@Override
public boolean equals(Object k) {
if(!(k instanceof cow)) {
return false;
}
cow c = (cow) k;
return (c.x == this.x) && (c.y == this.y);
}
}