【最长上升子序列】杭电 hdu 1257 最少拦截系统

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

    URL   : http://acm.hdu.edu.cn/showproblem.php?pid=1257
    Name  : 1257 最少拦截系统

    Date  : Saturday, July 07, 2012
    Time Stage : 1 hour and a half

    Result:
6147741	2012-07-07 16:42:20	Accepted	1257
15MS	272K	1480 B
C++	pyy


Test Data :

Review :
本来是求最长非升子升序的。但是因为题目要求最少套数,便难了。我想用搜索做或许会容易理解。而用枚举dp的结果的话,很难实现。于是思维陷入困境,只好百度之了。请进入传送门~:
http://blog.csdn.net/cclsoft/article/details/4601855
不过,貌似用贪心也可以做:
http://hi.baidu.com/song19870626/blog/item/c209f8128180692bdc5401f2.html
感觉dp做这题确实有点绕弯子了。
//----------------------------------------------------------------------------*/

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

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

#define DB    /##/
typedef __int64	LL;

int dp[MAXN], a[MAXN];

int LongestIncSubSeq(int a[], int n)
{
	int i, j, longest;

	MEM(dp, 0);
	for (i = 0; i < n; ++i)
	{
		dp[i] = 1;
		for (j = 0; j < i; ++j)
		{
			if (a[i] > a[j])
				dp[i] = max(dp[i], dp[j]+1);
		}
	}

	longest = 0;
	for (i = 0; i < n; ++i)
		longest = max(longest, dp[i]);

	return longest;
}

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

写代码的安徒生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值