[Link] https://www.acmicpc.net/problem/14003
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
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader br;
static int max = 0, maxIdx = 0;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String[] line;
int n = toi(br.readLine());
line = getLine();
int[] dp = new int[n];
int max = 0, maxIdx = 0;
int arr[] = new int[n];
ArrayList<Integer> al = new ArrayList<>();
Arrays.fill(dp, -1);
al.add(toi(line[0]));
arr[0] = toi(line[0]);
dp[0] = 0;
for(int i = 1; i < n; i++) {
int v = toi(line[i]), left = 0, right = al.size() - 1, mid;
arr[i] = v;
if(al.get(al.size() - 1) < v) {
al.add(v);
maxIdx = i;
dp[i] = al.size() - 1;
continue;
}
while(left <= right) {
mid = (left + right) / 2;
if(al.get(mid) >= v) right = mid - 1;
else left = mid + 1;
}
al.set(right + 1, v);
dp[i] = right + 1;
}
sb.append(dp[maxIdx] + 1).append("\n");
Stack<Integer> stack = new Stack<>();
stack.push(arr[maxIdx]);
int val = dp[maxIdx] - 1;
for(int i = maxIdx - 1; i >= 0; i--) {
if(dp[i] == val) {
stack.push(arr[i]);
val--;
}
if(val == -1) break;
}
while(!stack.isEmpty()) { sb.append(stack.pop()).append(" "); }
print(sb);
}
static int toi(String s) { return Integer.parseInt(s); }
static String[] getLine() throws IOException { return br.readLine().split(" "); }
static <T> void print(T s) { System.out.print(s); }
static <T> void println(T s) { System.out.println(s); }
}