-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCowJogSimplified.java
More file actions
43 lines (42 loc) · 1.46 KB
/
CowJogSimplified.java
File metadata and controls
43 lines (42 loc) · 1.46 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
import java.io.*;
public class CowJogSimplified {
public static void main(String[] args) throws IOException
{
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new FileReader("cowjog.in"));
String[] temp = br.readLine().split(" ");
int N = Integer.parseInt(temp[0]);
int T = Integer.parseInt(temp[1]);
long[][] segments = new long[N][2];
long x1, x2, v;
for (int i = 0; i < N; i++) // segments already given in increasing order
{
temp = br.readLine().split(" "); // of starting point
x1 = Long.parseLong(temp[0]);
v = Long.parseLong(temp[1]);
x2 = v*T+x1;
segments[i] = new long[] {x1, x2};
}
long min = Long.MAX_VALUE;
int groups = 0;
for (int i = N-1; i>-1; i--)
{
if (segments[i][1] < min)
groups++;
min = Math.min(segments[i][1], min);
}
//System.out.println(groups);
/*Explanation from USACO:
* Processing the segments backward from decreasing starting point
* keep track of the point closest to the start, out of the cows we've seen
* so far, this represents the slowest cow. Any time a cow segment ends after
* this value, this cow is absorbed with the slower cow.
* If a new min is established, this cow is independent and never overlaps
* our previous min. Now we compare against this one.
*/
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("cowjog.out")));
pw.println(groups);
pw.close();
br.close();
}
}