-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeasuringTraffic.java
More file actions
93 lines (83 loc) · 2.52 KB
/
MeasuringTraffic.java
File metadata and controls
93 lines (83 loc) · 2.52 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
import java.io.*;
import java.util.*;
public class MeasuringTraffic {
// An arbitrary number that's large compared to the ones in the problem.
static final int LARGE = (int)1e9;
public static void main(String[] args) throws IOException {
Kattio io = new Kattio("traffic");
int numMiles = io.nextInt();
String[] segmentType = new String[numMiles];
int[] start = new int[numMiles];
int[] end = new int[numMiles];
for (int m = 0; m < numMiles; m++) {
segmentType[m] = io.next();
start[m] = io.nextInt();
end[m] = io.nextInt();
}
// Set a large range that might as well be [0, infinity)
int low = 0;
int high = LARGE;
for (int m = numMiles - 1; m >= 0; m--) {
if (segmentType[m].equals("none")) {
// Set a new range based on sensor reading.
low = Math.max(low, start[m]);
high = Math.min(high, end[m]);
} else if (segmentType[m].equals("off")) {
// Update range of possible traffic flows
low += start[m];
high += end[m];
} else if (segmentType[m].equals("on")) {
low -= end[m];
high -= start[m];
// Set to zero if low becomes negative
low = Math.max(0, low);
}
}
io.println(low + " " + high);
low = 0;
high = LARGE;
// Process again, this time scanning the other way
for (int m = 0; m < numMiles; m++) {
if (segmentType[m].equals("none")) {
low = Math.max(low, start[m]);
high = Math.min(high, end[m]);
} else if (segmentType[m].equals("on")) {
low += start[m];
high += end[m];
} else if (segmentType[m].equals("off")) {
low -= end[m];
high -= start[m];
low = Math.max(0, low);
}
}
io.println(low + " " + high);
io.close();
}
static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio() { this(System.in, System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio(String problemName) throws IOException {
super(problemName + ".out");
r = new BufferedReader(new FileReader(problemName + ".in"));
}
// returns null if no more input
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) { }
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
}