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

【最长上升子序列】杭电 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;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics