POJ 1887 —— Testing the CATCHER 最长下降子序列

原题:http://poj.org/problem?id=1887

求最长下降子序列个数

注意输出格式!


O(n²)

#include
#include

using namespace std;
int dp[100000000];
int a[100000000];
int cas = 0;
int main()
{
	int tmp;
	while(~scanf("%d", &tmp))
	{
		if(tmp == -1)	break;
		int cnt = 0;
		a[cnt++] = tmp;
		while(1)
		{
			scanf("%d", &tmp);
			if(tmp == -1)	break;
			a[cnt++] = tmp;
		}
		for(int i = 0;i



O(nlogn)

#include
#define inf 99999999
int stack[100000000];
int cas = 0;

int main()
{
	int tmp;
	while(~scanf("%d", &tmp))
	{
		if(tmp == -1)	break;
		int top = 1;
		stack[1] = tmp;
		while(1)
		{
			scanf("%d", &tmp);
			if(tmp == -1)	break;
			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])
						r = mid - 1;
					else
						l = mid + 1;
				}
				stack[l] = tmp;
			}	
		}		
		printf("Test #%d:\n", ++cas);
		printf("  maximum possible interceptions: %d\n\n",top);
	}
	return 0;
}





你可能感兴趣的:(最长上升子序列)