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

【最长下降子序列】北大 poj 1952 BUY LOW, BUY LOWER

 
阅读更多


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

    URL   : http://poj.org/problem?id=1952
    Name  : 1952 BUY LOW, BUY LOWER

    Date  : Tuesday, July 10, 2012
    Time Stage : 2 hours

    Result:
0411693	panyanyany
1952
Accepted	224K	172MS	C++
1970B	2012-07-10 09:44:02

Test Data :

Review :
参考:
http://blog.acmj1991.com/?p=606
//----------------------------------------------------------------------------*/

#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	(5005)

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

#define DB    //

int sum, a[MAXN], dp[MAXN], route[MAXN];

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

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

		// 去重,比如 5 2 3 2,当 i==3时,计算到第2个2,那么对于以后的数来
		// 说,第1个2的route就是多余的了。因为凡是比2小的数,比如1,显然跟第2个2
		// 比较更好,因为(dp[3] = 3) > (dp[1] = 2)。然后再普及到一般的情况:
		// 5 2 1 2, 此时两个2的route和dp都一样,所以第一个2多余.
		for (j = 0; j < i; ++j)
			if (a[i] == a[j])
				route[j] = 0;
	}

	j = 0;
	for (i = 0; i < n; ++i)
	{
		if (dp[j] < dp[i])
			j = i;
	}
	for (i = 0; i < n; ++i)
		if (i != j && dp[j] == dp[i])
			route[j] += route[i];
	return j;
}

int main()
{
	int i, j, n;
	while (scanf("%d", &n) != EOF)
	{
		for (i = 0; i < n; ++i)
			scanf("%d", a+i);
		j = LIS(a, n);
		printf("%d ", dp[j]);
		printf("%d\n", route[j]);
	}
	return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics