Problem:
Score : 500 points

Problem Statement

You are given a sequence with N integers: A = \{ A_1, A_2, \cdots, A_N \}.
For each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:

If A_i and A_j (i < j) are painted with the same color, A_i < A_j.

Find the minimum number of colors required to satisfy the condition.

Constraints

1 \leq N \leq 10^5

0 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the minimum number of colors required to satisfy the condition.

Sample Input 1

5
2
1
4
5
3

Sample Output 1

2

We can satisfy the condition with two colors by, for example, painting 2 and 3 red and painting 1, 4, and 5 blue.

Sample Input 2

4
0
0
0
0

Sample Output 2

4

We have to paint all the integers with distinct colors.

Solution in Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::fs::File;
#[allow(unused_imports)]
use std::io::prelude::*;
#[allow(unused_imports)]
use std::io::Stdin;
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufReader, BufWriter, Write};
#[allow(unused_imports)]
use std::mem::swap;
#[allow(unused_imports)]
use std::str::FromStr;

type BoxResult<T> = Result<T, Box<std::error::Error>>;

fn main() {
    let mut scanner = Scanner::new(stdin());
    let n = scanner.read::<usize>().unwrap();
    let mut arr = vec![];
    for _ in 0..n {
        let num = scanner.read::<usize>().unwrap();
        arr.push(num);
    }
    let mut unique = arr.clone();
    unique.sort();
    unique.dedup();
    let convert_map = (0..unique.len()).map(|i| (unique[i], i + 1)).collect::<HashMap<_, _>>();
    let converted_arr = arr.into_iter().map(|num| convert_map[&num]).collect::<Vec<_>>();
    let mut dp = vec![0; converted_arr.len()];
    for i in 0..n {
        let index = binary_search(converted_arr[i], &dp);
        dp[index] = converted_arr[i];
    }
    for i in (0..n).rev() {
        if dp[i] > 0 {
            println!("{}", i + 1);
            return;
        }
    }
}

fn binary_search(x: usize, arr: &[usize]) -> usize {
    if x > arr[0] {
        return 0;
    }
    if x <= arr[arr.len() - 1] {
        return arr.len();
    }
    let (mut l, mut r) = (0, arr.len() - 1);
    while r - l > 1 {
        let mid = (l + r) / 2;
        if x <= arr[mid] {
            l = mid;
        } else {
            r = mid;
        }
    }
    r
}


#[allow(dead_code)]
struct Scanner<R> {
    reader: BufReader<R>,
    buffer: String,
}

impl<R> Scanner<R>
where
    R: Read,
{
    #[allow(dead_code)]
    fn new(inner: R) -> Scanner<R> {
        let reader = BufReader::new(inner);
        let buffer = String::new();
        Scanner { reader: reader, buffer: buffer }
    }

    #[allow(dead_code)]
    fn with_capacity(capacity: usize, inner: R) -> Scanner<R> {
        let reader = BufReader::with_capacity(capacity, inner);
        let buffer = String::new();
        Scanner { reader: reader, buffer: buffer }
    }

    #[allow(dead_code)]
    fn read<T>(&mut self) -> BoxResult<T>
    where
        T: FromStr,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        self.reader.read_line(&mut self.buffer)?;
        let parsed = self.buffer.trim().parse()?;
        self.buffer.clear();
        Ok(parsed)
    }

    #[allow(dead_code)]
    fn read_vector<T>(&mut self) -> BoxResult<Vec<T>>
    where
        T: FromStr,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        self.reader.read_line(&mut self.buffer)?;
        let parsed_vec = self
            .buffer
            .trim()
            .split_whitespace()
            .map(|s| s.parse())
            .collect::<Result<Vec<_>, _>>()?;
        self.buffer.clear();
        Ok(parsed_vec)
    }

    #[allow(dead_code)]
    fn read_matrix<T>(&mut self, lines: usize) -> BoxResult<Vec<Vec<T>>>
    where
        T: FromStr,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        let mut parsed_matrix = vec![];
        for _ in 0..lines {
            self.reader.read_line(&mut self.buffer)?;
            parsed_matrix.push(
                self.buffer
                    .trim()
                    .split_whitespace()
                    .map(|s| s.parse())
                    .collect::<Result<Vec<_>, _>>()?,
            );
            self.buffer.clear();
        }
        Ok(parsed_matrix)
    }
}