-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestingTheCATCHER.java
66 lines (63 loc) · 1.65 KB
/
TestingTheCATCHER.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
58
59
60
61
62
63
64
65
66
/** #dynamic-programming #lis */
import java.util.ArrayList;
import java.util.Scanner;
class TestingTheCATCHER {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcase = 1;
int first, next;
while (sc.hasNextInt()) {
first = sc.nextInt();
if (first == -1) {
break;
}
if (testcase > 1) {
System.out.println();
}
ArrayList<Integer> list = new ArrayList<>();
list.add(first);
while (sc.hasNextInt()) {
next = sc.nextInt();
if (next == -1) {
break;
}
list.add(next);
}
int result = LIS(list);
System.out.println("Test #" + (testcase++) + ":");
System.out.println(" maximum possible interceptions: " + result);
}
}
public static int LIS(ArrayList<Integer> list) {
int len = 1;
ArrayList<Integer> result = new ArrayList<>();
result.add(0);
for (int i = 1; i < list.size(); i++) {
if (list.get(i) > list.get(result.get(0))) {
result.set(0, i);
} else if (list.get(i) <= list.get(result.get(len - 1))) {
result.add(i);
len++;
} else {
int pos = lowerBound(list, result, len, list.get(i));
result.set(pos, i);
}
}
return len;
}
public static int lowerBound(ArrayList<Integer> list, ArrayList<Integer> sub, int n, int v) {
int left = 0, right = n;
int pos = n;
while (left < right) {
int mid = left + (right - left) / 2;
int idx = sub.get(mid);
if (list.get(idx) < v) {
pos = mid;
right = mid;
} else {
left = mid + 1;
}
}
return pos;
}
}