• + 0 comments
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Solve_problems {
    
        static void solve(int n, int[] array) {
            int[] tails = new int[n];
            int len = 1;
    
            tails[0] = array[0];
    
            for (int i = 1; i < n; i++) {
                if (array[i] < tails[0]) {
                    // New smallest value
                    tails[0] = array[i];
                } else if (array[i] > tails[len - 1]) {
                    // Array[i] extends the largest subsequence
                    tails[len++] = array[i];
                } else {
                    // Array[i] will become end candidate of an existing
                    // subsequence or replace a candidate in the middle
                    int pos = Arrays.binarySearch(tails, 0, len, array[i]);
                    if (pos < 0) {
                        pos = -pos - 1;
                    }
                    tails[pos] = array[i];
                }
            }
    
            System.out.println(len);
        }
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            // Input
            int n = sc.nextInt();
            int[] array = new int[n];
            for (int i = 0; i < n; i++)
                array[i] = sc.nextInt();
    
            // Solve the problem
            solve(n, array);
    
            // Close the Scanner instance
            sc.close();
        }
    }