`
txf2004
  • 浏览: 6865943 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

【最长上升子序列】北大 POJ 1631 Bridging signals

 
阅读更多


/* THE PROGRAM IS MADE BY PYY */
/*----------------------------------------------------------------------------//
    Copyright (c) 2012 panyanyany All rights reserved.

    URL   : http://poj.org/problem?id=1631
    Name  : 1631 Bridging signals
	Classification : 最长上升子序列

    Date  : Wednesday, July 11, 2012
    Time Stage : half an hour

    Result:
10420085	panyanyany
1631
Accepted	472K	125MS	C++
1557B	2012-07-11 07:29:31

Test Data :

Review :

//----------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>

using namespace std ;

#define MEM(a, v)        memset (a, v, sizeof (a))    // a for address, v for value
#define max(x, y)        ((x) > (y) ? (x) : (y))
#define min(x, y)        ((x) < (y) ? (x) : (y))

#define INF     (0x3f3f3f3f)
#define MAXN	(40005)

#define L(x)	((x)<<1)
#define R(x)	(((x)<<1)|1)
#define M(x, y)	(((x)+(y)) >> 1)

#define DB    //

int a[MAXN], order[MAXN];

int LIS(int a[], int n)
{
	int i, u, l, m, len;

	len = 1;
	MEM(order, INF); //	找上升子序列时初始化 INF,找下降子序列时初始化为0
	for (i = 0; i < n; ++i)
	{
		u = len;
		l = 1;
		while (l <= u)
		{
			m = (l + u) >> 1;
			if (order[m] < a[i])
				l = m + 1;
			else
				u = m - 1;
		}
		if (order[l] > a[i])
			order[l] = a[i];
		if (len < l)
			len = l;
//		printf("^ %d ^", l);
	}

	return len;
}

int main()
{
	int i, n, tc;
	while (scanf("%d", &tc) != EOF)
	{
		while (tc--)
		{
			scanf("%d", &n);
			for (i = 0; i < n; ++i)
				scanf("%d", a+i);
			printf("%d\n", LIS(a, n));
		}
	}
	return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics