Stock Exchange

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);
	}
}

 

全部评论

相关推荐

2025-12-16 17:17
门头沟学院 产品经理
烤点老白薯:他第二句话的潜台词是想让你帮他点个瑞幸或者喜茶啥的
mt对你说过最有启发的一...
点赞 评论 收藏
分享
合适才能收到offe...:项目岗是什么岗?我看你有段好像跟策划运营相关,如果找运营的话第三段经历写详细点儿。 个人建议是把自我评价删了换成专业技能放在工作经验上或者下面。学生会那个也可以删,把第一个包装成店铺运营,写4-6给点。第三个也是写4-6个点。注意工作内容➕部分数据。 投递的时候BOS招呼用语改一下,换成我有xx工作经验,熟练掌握xx技能样式,也可以简历截图然后直接发送。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务