-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhyCrossTheRoad.java
More file actions
55 lines (53 loc) · 1.87 KB
/
Copy pathWhyCrossTheRoad.java
File metadata and controls
55 lines (53 loc) · 1.87 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
import java.util.*;
import java.io.*;
public class WhyCrossTheRoad {
public static void main(String[] args) throws IOException {
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new FileReader("helpcross.in"));
//PrintWriter pw = new PrintWriter(System.out);
PrintWriter pw = new PrintWriter("helpcross.out");
StringTokenizer st = new StringTokenizer(br.readLine());
int C = Integer.parseInt(st.nextToken());
int N = Integer.parseInt(st.nextToken());
int[] chickens = new int[C];
for(int i=0; i<C; i++) chickens[i] = Integer.parseInt(br.readLine());
Cow[] cows = new Cow[N];
for(int i=0; i<N; i++) {
st = new StringTokenizer(br.readLine());
cows[i] = new Cow(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
}
Arrays.sort(cows);
Arrays.sort(chickens);
br.close();
int numHelped = 0;
int cowAt = 0;
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int c : chickens) {
while(cowAt < N && cows[cowAt].start <= c) {
pq.add(cows[cowAt].end);
cowAt++;
}
while(!pq.isEmpty() && pq.peek() < c) {
pq.remove();
}
if(!pq.isEmpty()) {
numHelped++;
pq.remove();
}
}
pw.println(numHelped);
pw.close();
}
static class Cow implements Comparable<Cow> {
int start;
int end;
public Cow(int start, int end) {
this.start = start;
this.end = end;
}
public int compareTo(Cow other) {
if(this.start == other.start) return this.end - other.end;
return this.start - other.start;
}
}
}