POJ 1631 —— Bridging signals 最長上升子序列

原題:http://poj.org/problem?id=1631

題意:有n個數,求最長上升子序列的個數;


#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 40000+10;
int stack[maxn];
int cas, n;

int main()
{
	scanf("%d", &cas);
	while(cas--)
	{
		scanf("%d", &n);
		int top = 0;
		stack[top] = -1;
		for(int i = 1;i<=n;i++)
		{
			int tmp;
			scanf("%d", &tmp);
			if(tmp > stack[top])
				stack[++top] = tmp;
			else
			{
				int l = 1, r = top;
				int mid;
				while(l <= r)
				{
					mid = (l+r)/2;
					if(tmp > stack[mid])
						l = mid+1;	
					else
						r = mid-1;
				}
				stack[l] = tmp;
			}
		}
		printf("%d\n", top);
	}	
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章