-
Notifications
You must be signed in to change notification settings - Fork 4
/
IsBiggerSmarter.java
67 lines (60 loc) · 2.05 KB
/
IsBiggerSmarter.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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class IsBiggerSmarter {
private static final BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
public static int[] solve(List<int[]> input) {
if (input.size() == 1) {
return new int[] { 0 };
}
List<int[]> sorted = new ArrayList<>(input);
Collections.sort(sorted, (x, y) -> Integer.compare(x[0], y[0]));
int[] res = new int[sorted.size()];
int[] ref = new int[sorted.size()];
int index = 0;
for (int i = 0; i < sorted.size(); ++i) {
res[i] = 1;
ref[0] = -1;
for (int j = 0; j < i; ++j) {
if (sorted.get(j)[0] < sorted.get(i)[0] &&
sorted.get(j)[1] > sorted.get(i)[1]) {
if (res[i] < res[j] + 1) {
res[i] = res[j] + 1;
ref[i] = j;
if (index == -1 || res[i] > res[index]) {
index = i;
}
}
}
}
}
int[] solution = new int[res[index]];
int i = solution.length - 1;
do {
solution[i] = sorted.get(index)[2];
index = ref[index];
i--;
} while (index != -1 && i >= 0);
return solution;
}
public static void main(String[] args) throws Exception {
String currentLine;
List<int[]> input = new ArrayList<>();
int i = 0;
while ((currentLine = reader.readLine()) != null) {
Scanner s = new Scanner(currentLine);
input.add(new int[] { s.nextInt(), s.nextInt(), i });
s.close();
i++;
}
int[] solution = solve(input);
System.out.println(solution.length);
for (int idx : solution) {
System.out.println(idx + 1);
}
}
}