/* 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;
}
【最长上升子序列】杭电 hdu 1257 最少拦截系统
最新推荐文章于 2020-08-19 14:07:24 发布