|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.StringTokenizer; |
| 6 | + |
| 7 | +public class BJ_12738_가장_긴_증가하는_부분_수열_3 { |
| 8 | + |
| 9 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + private static StringTokenizer st; |
| 12 | + |
| 13 | + private static int N; |
| 14 | + private static int[] arr; |
| 15 | + private static List<Integer> list; |
| 16 | + |
| 17 | + public static void main(String[] args) throws IOException { |
| 18 | + init(); |
| 19 | + sol(); |
| 20 | + } |
| 21 | + |
| 22 | + private static void init() throws IOException { |
| 23 | + N = Integer.parseInt(br.readLine()); |
| 24 | + |
| 25 | + arr = new int[N]; |
| 26 | + st = new StringTokenizer(br.readLine()); |
| 27 | + for (int i = 0; i < N; i++) { |
| 28 | + arr[i] = Integer.parseInt(st.nextToken()); |
| 29 | + } |
| 30 | + list = new ArrayList<>(); |
| 31 | + } |
| 32 | + |
| 33 | + private static void sol() throws IOException { |
| 34 | + list.add(Integer.MIN_VALUE); |
| 35 | + |
| 36 | + for (int a : arr) { |
| 37 | + int size = list.size() - 1; |
| 38 | + |
| 39 | + if (list.get(size) < a) { |
| 40 | + list.add(a); |
| 41 | + } else { |
| 42 | + int index = binarySearch(0, size, a); |
| 43 | + |
| 44 | + list.set(index, a); |
| 45 | + } |
| 46 | + } |
| 47 | + bw.write(list.size() - 1 + ""); |
| 48 | + bw.flush(); |
| 49 | + bw.close(); |
| 50 | + br.close(); |
| 51 | + } |
| 52 | + |
| 53 | + private static int binarySearch(int left, int right, int target) { |
| 54 | + while (left < right) { |
| 55 | + int mid = left + (right - left) / 2; |
| 56 | + if (list.get(mid) < target) { |
| 57 | + left = mid + 1; |
| 58 | + } else { |
| 59 | + right = mid; |
| 60 | + } |
| 61 | + } |
| 62 | + return right; |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | +```` |
0 commit comments