-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLISEfficient.java
More file actions
34 lines (28 loc) · 1013 Bytes
/
LISEfficient.java
File metadata and controls
34 lines (28 loc) · 1013 Bytes
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
import java.util.*;
import java.io.*;
//Finding Longest Increasing Subsequence with TreeMap
//Replace ceilingKey with higherKey to get Longest Non-Decreasing Subsequence
public class LISEfficient {
public static void main(String[] args) {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
TreeMap<Integer, Integer> seq = new TreeMap<>();
int size = 0;
for (int i = 0; i < n; i++) {
int a = in.nextInt();
Integer ceil = seq.ceilingKey(a);
if (ceil != null && seq.get(ceil) != 0) {
seq.put(ceil, seq.get(ceil) - 1);
if (seq.get(ceil) == 0) seq.remove(ceil);
size--;
//out.println(ceil + "->" + a);
}
seq.put(a, seq.getOrDefault(a, 0) + 1);
size++;
//out.println(seq);
}
out.println("Size: " + size);
out.close();
}
}