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_timeout.java
More file actions
116 lines (108 loc) · 3.03 KB
/
convention2_timeout.java
File metadata and controls
116 lines (108 loc) · 3.03 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.io.*;
import java.util.*;
public class convention2_timeout {
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;
cow c2;
boolean first =true;
for(int i = 0; i < time.size(); i ++) {
System.out.println("Completed "+i+" of "+time.size()+" <- Value may change but N is "+N);
if(theline.isEmpty()) {
tc = time.get(i);// Short for the cow
}else {
//System.out.println(tc.x +" " + tc.y +" "+ theline.get(0).x+" "+theline.get(0).y);
c2 = theline.get(0);
boolean nodouble = c2.equals(tc);
if(nodouble) {
i --; // Make sure loop runs again
time.remove(c2); // Remove cow
tc = theline.remove(0); // Let the waiting cow eat
continue;
}
ctime = tc.x + tc.y - c2.x;
//System.out.println("Achieved time of "+ctime);
if(ctime > mtime) {
if(!nodouble) {
//System.out.println("New range");
mtime = ctime;
}
}
i --; // Make sure loop runs again
time.remove(c2); // Remove cow
//senority.remove(theline.get(0));
tc = theline.remove(0); // Let the waiting cow eat
tc.x = tc.x + ctime;
}
c1 = tc.x + tc.y;
for(int j = i + 1; j < time.size(); j ++) {
if (time.get(i).x > time.get(j).x) {
//break;
continue; // On second thought
//throw new IOException("Something impossible just happeneded.");
}
if(time.get(j).x > c1) {
break;
}
theline.add(time.get(j));
theline.sort(cowcompare);
}
if(i == 0 && first) {
//System.out.println(theline.remove(0));
}
System.out.println(theline);
first = false;
}
//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);
}
}