【最大上升子序列+经典题】北大 poj 2533 Longest Ordered Subsequence

本文介绍了一种解决最长递增子序列问题的有效算法,并通过一个C++程序实现了该算法。该程序首先初始化动态规划数组,然后遍历输入序列,对于每个元素,它会检查之前的所有元素以确定最长递增子序列的长度。最终输出最长递增子序列的长度。

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


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

    URL   : http://poj.org/problem?id=2533
    Name  : 2533 Longest Ordered Subsequence

    Date  : Sunday, July 8, 2012
    Time Stage : half an hour

    Result:
10398877	panyanyany
2533
Accepted	172K	16MS	C++
1400B	2012-07-08 11:40:02

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	1009

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

#define DB    //

int dpInc[MAXN], a[MAXN];

int LIS(int a[], int n)
{
	int i, j;
	for (i = 0; i < n; ++i)
	{
		dpInc[i] = 1;
		for (j = 0; j < i; ++j)
		{
			if (a[j] < a[i])
				dpInc[i] = max(dpInc[i], dpInc[j] + 1);
		}
	}

	j = 0;
	for (i = 0; i < n; ++i)
		j = max(j, dpInc[i]);

	return j;
}

int main()
{
	int i, n;
	while (scanf("%d", &n) != EOF)
	{
		for (i = 0; i < n; ++i)
			scanf("%d", a+i);

		printf("%d\n", LIS(a, n));
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值