【最长非升子序列+nlog(n)】北大 POJ 3636 Nested Dolls

本文详细介绍了如何解决3636 Nested Dolls问题,即求解最长非升子序列。通过使用排序和动态规划方法,作者成功解决了这一经典算法问题,并提供了具体实现代码和优化思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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

    URL   : http://poj.org/problem?id=3636
    Name  : 3636 Nested Dolls – 最长非升子序列

    Date  : Monday, July 9, 2012
    Time Stage : two hours

    Result:
10404790	panyanyany
3636
Accepted	400K	157MS	C++
1827B	2012-07-09 11:01:39

Test Data :

Review :
直接跳到神牛的博客吧:
http://hi.baidu.com/findthegateopen/blog/item/8d7694127d16b7d8f7039eb1.html
//----------------------------------------------------------------------------*/

#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	20009

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

#define DB    //

struct NODE {
	int w, h;
};

bool cmp(const NODE &lhs, const NODE &rhs)
{
	if (lhs.w == rhs.w)
		return lhs.h > rhs.h;
	return lhs.w < rhs.w;
}

int order[MAXN];
NODE a[MAXN];

int LNotIncS(NODE a[], int n)
{
	int i, r, l, len, m;

	MEM(order, 0);
	sort(a, a+n, cmp);

	len = 1;

	for (i = 0; i < n; ++i)
	{
		r = len;
		l = 1;
		while (l <= r)
		{
			m = (l + r) >> 1;
			if (order[m] >= a[i].h)
			{
				l = m + 1;
			}
			else
				r = m - 1;
		}

		// 更新有序表里面的长度为 L 的最大值.
		// 如果有序表是上升的,则更新的是最小值
		if (order[l] < a[i].h)
			order[l] = a[i].h;
		len = max(len, l);
	}

	return len;
}

int main()
{
	int i, tcase, n;
	while (scanf("%d", &tcase) != EOF)
	{
		while (tcase--)
		{
			scanf("%d", &n);
			for (i = 0; i < n; ++i)
			{
				scanf("%d %d", &a[i].w, &a[i].h);
			}
			printf("%d\n", LNotIncS(a, n));
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值