The world financial crisis is quite a subject. Some people are more relaxed while others are quite anxious. John is one of them. He is very concerned about the evolution of the stock exchange. He follows stock prices every day looking for rising trends. Given a sequence of numbers p1, p2,...,pn representing stock prices, a rising trend is a subsequence pi1 < pi2 < ... < pik, with i1 < i2 < ... < ik. John’s problem is to find very quickly the longest rising trend.

Input

Each data set in the file stands for a particular set of stock prices. A data set starts with the length L (L ≤ 100000) of the sequence of numbers, followed by the numbers (a number fits a long integer). 
White spaces can occur freely in the input. The input data are correct and terminate with an end of file.

Output

The program prints the length of the longest rising trend. 
For each set of data the program prints the result to the standard output from the beginning of a line.

Sample Input

6 
5 2 1 4 5 3 
3  
1 1 1 
4 
4 3 2 1

Sample Output

3 
1 
1

Hint

There are three data sets. In the first case, the length L of the sequence is 6. The sequence is 5, 2, 1, 4, 5, 3. The result for the data set is the length of the longest rising trend: 3.

最长上升子序列理解可参见:http://blog.csdn.net/code_pang/article/details/8757380

C++版本一

 

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int n,m;
int a[110000];
int dp[110000];
//二分查找,可以用函数lower_bound 代替
int BSearch(int a[], int n, int t)
{
	int low = 1;
	int high = n;

	while (low <= high)
	{
		int mid = (low + high) / 2;
		if (t == a[mid])
		{
			return mid;
		}
		else if (t > a[mid])
		{
			low = mid + 1;
		}
		else
		{
			high = mid - 1;
		}
	}
	return low;
}


int LIS_BSearch(int a[], int m[], int n)
{
	int maxlen = 1;		//最长上升子序列的长度
	m[maxlen] = a[1];

	int i;
	for (i = 2; i <= n; i++)
	{
		if (a[i] > m[maxlen])
		{
			m[++maxlen] = a[i];
		}
		else
		{
			//返回小于a[i]的最大值的位置p
			int p = BSearch(m, maxlen, a[i]);
			m[p] = a[i];
		}
	}
	return maxlen;

}

int main()
{
    while(scanf("%d",&n)!=EOF){
        if(n==0&&m==0)    break;
        memset(dp,1,sizeof(dp));
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        cout << LIS_BSearch(a, dp, n) << endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二

#include<stdio.h>
const int maxn=100005;
long long a[maxn];
int dp[maxn];//dp[i]指的是 i为长度的子序列的末尾数的最小值 
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		for(int i=0; i<n; i++)
			scanf("%lld",&a[i]);
		dp[1]=a[0];//长度为一的子序列的末尾最小值  初始化为a[0] 
		int maxlen=1;
		for(int i=1; i<n; i++)
		{
			int l=0;
			int r=maxlen,mid;
			if(dp[maxlen]<a[i])//若此时最大长度的子序列的最小值 仍小于a[i] 则最大长度加一 
				dp[++maxlen]=a[i];
			else
			{
				r=maxlen;
				while(l<=r)
				{
					mid=(l+r)/2;
					if(dp[mid]<a[i])
						l=mid+1;
					else
						r=mid-1;
				}
				//找到第一个大于a[i]的数 更新其的值 使其值更小 
				dp[l]=a[i];
			}
		}
	/*	for(int i=0; i<n; i++)
			printf("%d ",dp[i]);
		printf("\n");*/
		printf("%d\n",maxlen);
	}
}

C++版本三

#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=100005;
long long a[maxn];
int dp[maxn];//dp[i]指的是 i为长度的子序列的末尾数的最小值
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		for(int i=0; i<n; i++)
			scanf("%lld",&a[i]);
		int maxlen=0;
		for(int i=0; i<n; i++)
		{
			int pos = lower_bound(dp, dp + maxlen, a[i]) - dp;
			if(pos == maxlen) dp[maxlen++] = a[i];
			else dp[pos] = a[i];
		}
		printf("%d\n",maxlen);
	}
}