poj 2533

参考blog:http://blog.csdn.net/yysdsyl/article/details/4264650?reload

    http://www.slyar.com/blog/longest-ordered-subsequence.html

    http://www.slyar.com/blog/poj-2533-cpp.html

时间复杂度N^2

#include <stdio.h>
#include <string.h>
#define Max(a,b) a>b?a:b
int main()
{
	int input[1005],M[1005];
	int N;
	int max = 0;

	scanf("%d",&N);
	for(int i = 0;i < N;++i)scanf("%d",&input[i]);

	for(int i = 0;i < N;++i) M[i] = 1;

	for(int i = 0; i < N;++i)
	{
		for(int j = 0; j < i;++j)
		{
			if(input[j] < input[i]) M[i] = Max(M[i],M[j] + 1);
		}
		//M[i]存的是前i个的最长升序子序列,而最大的不一定是在M[N-1]
		if(max < M[i]) max = M[i];
	}
	//printf("%d\n",M[N-1]);
	printf("%d\n",max);

	return 0;
}


时间复杂度NlgN

#include <stdio.h>

int qurery_binary(int *array,int cmp,int begin,int end)
{
	//[begin,end]
	if(begin>end) return begin;
	int mid = (begin + end)/2;
	if(array[mid] > cmp) return qurery_binary(array,cmp,begin,mid-1);
	else if(array[mid] < cmp) return qurery_binary(array,cmp,mid+1,end);
	else return mid;
}


int main(int argc, char const *argv[])
{

	int stack[1005];
	int N,top = 0,sub,tmp;
	scanf("%d",&N);
	for(int i = 0;i < N;++i)
	{
		scanf("%d",&tmp);
		if(top != 0 && tmp <= stack[top-1])
		{
		    //if(tmp)
			sub = qurery_binary(stack,tmp,0,top-1);
			stack[sub] = tmp;
		}else{
			stack[top ++] = tmp;
		}
	}
	printf("%d\n",top);
	return 0;
}






你可能感兴趣的:(c,dp)