{POJ}{3903}{Stock Exchange}{nlogn 最長上升子序列}

題意:求最長上升子序列,n=100000ios

思路:O(N^2)鐵定超時啊。。。。利用貪心的思想去找答案。利用棧,每次輸入數據檢查棧,二分查找替換掉最小比他大的數據,這樣獲得的棧就是更優的。這個題目確實不錯,思路很好spa

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <memory>
#include <cmath>
#include <bitset>
#include <queue>
#include <vector>
#include <stack>
using namespace std;
 
 
#define CLR(x,y) memset(x,y,sizeof(x))
#define MIN(m,v) (m)<(v)?(m):(v)
#define MAX(m,v) (m)>(v)?(m):(v)
#define ABS(x) ((x)>0?(x):-(x))
#define rep(i,x,y) for(i=x;i<y;++i)


const int MAXN = 110000;

int n,m;
int len;
int val;
int s[MAXN];

int BF(int cur)
{
	int low,high,mid;
	int pre;

	low = 0;
	high = len-1;
	while(low <= high)
	{
		mid = (low+high)>>1;
		if(s[mid]<cur){
			low = mid+1;
		}
		else if(s[mid]>cur){
			high = mid-1;
		}
		else 
			return mid;
	}
	return low;
}
void Solve()
{
	while(scanf("%d",&n)!=EOF)
	{
		len = 0;
		for(int i = 0 ; i < n; ++i)
		{
			scanf("%d",&val);
			if(len == 0 || s[len-1] < val){
				s[len] = val;
				++len;
			}
			else
			{
				int f = BF(val);
				s[f] = val;
			}
		}

		printf("%d\n",len);
	}
}

int main()
{
	Solve();

	return 0;
}
相關文章
相關標籤/搜索