Problem:
Longest Increasing Subsequence

For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A.

An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik.

Input

n
a0
a1
:
an-1

In the first line, an integer n is given. In the next n lines, elements of A are given.

Output

The length of the longest increasing subsequence of A.

Constraints

1 ≤ n ≤ 100000

0 ≤ ai ≤ 109

Sample Input 1

5
5
1
3
2
4

Sample Output 1

3

Sample Input 2

3
1
1
1

Sample Output 2

1

Solution in C++ (judge verdict: Accepted)

#include <stdio.h>
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_D
//LIS
//got TLE with O(n*n)
/*int findNumberOfLIS(int nums[],int n) {
        int l = n;
        int lis[l];
        for(int i= 0; i<l; i++){//as the last one
            lis[i] = 1;//for 1
            for(int j = 0; j<i; j++){
                if(nums[i]>nums[j]&&lis[j]>=lis[i]){
                    lis[i] = lis[j]+1; 
                }
            }   
        }
        int res = 0;
        for(int i =0; i<l; i++){
            if(lis[i]>res)
                res = lis[i];
            
        }
        return res;  
    }*/
    
//search longest increasing sequence (geek for geek)
//binary search to find the first ele bigger than key
//return index of the ele    
int ceilindex(int sec_array[],int l,int r,int key){
	while(r-l>1){
		int m = l+(r-l)/2;
		if(sec_array[m]>=key)
			r = m;
		else l = m;
	}
	return r;
}
int findNumberOfLIS(int nums[],int n){
	int sec_array[n];
	int len;
	sec_array[0] = nums[0];
	len = 1;
	for(int i  =1; i<n; i++){
		if(nums[i]<sec_array[0])
			sec_array[0] = nums[i];
		else if(nums[i]>sec_array[len-1])
			sec_array[len++] = nums[i];
		else {
			sec_array[ceilindex(sec_array,-1,len-1,nums[i])] = nums[i];
		}
	}
	//the sec_array is not LIS: smallest elements
	/*for(int i = 0; i<len; i++){
		printf("%d ",sec_array[i]);
	}
	printf("\n");*/
	return len;
} 

int main(){
	int n;
	scanf("%d",&n);
	int nums[n];
	for(int i = 0; i<n; i++){
		scanf("%d",&nums[i]);
	}
	printf("%d\n",findNumberOfLIS(nums,n));
	return 0;
}