-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCD.java
57 lines (44 loc) · 1.55 KB
/
CD.java
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
/*
Problem: https://open.kattis.com/problems/cd
Author: Adrian Reithaug
Submitted: June 13th, 2017
Time: 1.04s / 3.00s
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CD {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String[] data = reader.readLine().split(" ");
int numJack = Integer.parseInt(data[0]);
int numJill = Integer.parseInt(data[1]);
if (numJack == 0 && numJill == 0) {
break;
}
int[] jackCatalog = new int[numJack];
for (int i = 0; i < numJack; i++) {
jackCatalog[i] = Integer.parseInt(reader.readLine());
}
int numEqual = 0;
for (int i = 0; i < numJill; i++) {
int cd = Integer.parseInt(reader.readLine());
int leftPos = 0;
int rightPos = numJack - 1;
while (leftPos <= rightPos) {
int mid = leftPos + ((rightPos - leftPos) / 2);
if (jackCatalog[mid] < cd) {
leftPos = mid + 1;
} else if (jackCatalog[mid] > cd) {
rightPos = mid - 1;
} else {
numEqual++;
break;
}
}
}
System.out.println(numEqual);
}
}
}