POJ 1952 BUY LOW, BUY LOWER

原创 2016年08月30日 17:45:15

Description

The advice to "buy low" is half the formula to success in the bovine stock market.To be considered a great investor you must also follow this problems' advice: 
                    "Buy low; buy lower"

Each time you buy a stock, you must purchase it at a lower price than the previous time you bought it. The more times you buy at a lower price than before, the better! Your goal is to see how many times you can continue purchasing at ever lower prices. 

You will be given the daily selling prices of a stock (positive 16-bit integers) over a period of time. You can choose to buy stock on any of the days. Each time you choose to buy, the price must be strictly lower than the previous time you bought stock. Write a program which identifies which days you should buy stock in order to maximize the number of times you buy. 

Here is a list of stock prices: 
 Day   1  2  3  4  5  6  7  8  9 10 11 12

Price 68 69 54 64 68 64 70 67 78 62 98 87


The best investor (by this problem, anyway) can buy at most four times if each purchase is lower then the previous purchase. One four day sequence (there might be others) of acceptable buys is: 
Day    2  5  6 10

Price 69 68 64 62

Input

* Line 1: N (1 <= N <= 5000), the number of days for which stock prices are given 

* Lines 2..etc: A series of N space-separated integers, ten per line except the final line which might have fewer integers. 

Output

Two integers on a single line: 
* The length of the longest sequence of decreasing prices 
* The number of sequences that have this length (guaranteed to fit in 31 bits) 

In counting the number of solutions, two potential solutions are considered the same (and would only count as one solution) if they repeat the same string of decreasing prices, that is, if they "look the same" when the successive prices are compared. Thus, two different sequence of "buy" days could produce the same string of decreasing prices and be counted as only a single solution. 

Sample Input

12
68 69 54 64 68 64 70 67 78 62
98 87

Sample Output

4 2


求最长严格下降子序列的长度以及本质不同的方案数。

先离散化,转换成求最长严格上升的,用L[i]来统计以i结尾的最长长度是多少。

用dp[i]来统计方案数,由于需要的是本质不同的,那么对于两个数a[i]和a[j]来说,

如果a[i]==a[j],那么我们显然只需要位置靠后的那个,前面那个是不需要统计的,

因为前面能形成的串必然可以被后面那个形成,这样就ok了。

顺便一说,统计方案数不需要大数,因为又要本质不同,又要长度最长,这样的方案数显然不可能很多。

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<string,string>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 5e3 + 10;
const int read()
{
	char ch = getchar();
	while (ch<'0' || ch>'9') ch = getchar();
	int x = ch - '0';
	while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
	return x;
}
int T, n, m, a[N], b[N];
int dp[N], L[N], f[N];

int main()
{
	while (scanf("%d", &n) != EOF)
	{
		rep(i, 1, n) scanf("%d", &a[i]), b[i] = a[i];
		sort(b + 1, b + n + 1); m = unique(b + 1, b + n + 1) - b;
		int ans = 0, len = 0;
		rep(i, 1, n)
		{
			a[i] = m - (lower_bound(b + 1, b + m, a[i]) - b);
			L[i] = 1;	dp[i] = 0;
			per(j, i - 1, 1) if (a[j] < a[i]) L[i] = max(L[i], L[j] + 1);
			len = max(L[i], len);
			if (L[i] == 1) { dp[i] = 1; continue; }
			rep(j, 1, m) f[j] = 0;
			per(j, i - 1, 1)
			{
				if (a[j] >= a[i] || L[i] != L[j] + 1) continue;
				if (f[a[j]]) continue; else f[a[j]] = 1;
				dp[i] += dp[j];
			}
		}
		rep(i, 1, m) f[i] = 0;
		per(i, n, 1)
		{
			if (f[a[i]]) continue; else f[a[i]] = 1;
			ans += (L[i] == len) * dp[i];
		}
		printf("%d %d\n", len, ans);
	}
	return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ 1952 BUY LOW,BUY LOWER 最长递减子序列 动态规划

POJ 1952 BUY LOW,BUY LOWER题目描述:  题目链接:POJ 1952 BUY LOW,BUY LOWER题目大意:  题目背景是股市,要求在给定的价格序列中求每次购买价格都下降...

poj1952--BUY LOW, BUY LOWER题解

来源:http://poj.org/problem?id=1952 思路:此题第一问是要求最长递减数列的长度,这问用DP很容易解决,但关键是第二问要求最长递减数列的个数,下面直接贴代码。 代码:#...
  • ME495
  • ME495
  • 2015年07月22日 23:41
  • 225

poj 1952 BUY LOW, BUY LOWER(求最长下降子序列个数)

BUY LOW, BUY LOWER Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7475   Accepte...
  • WEYuLi
  • WEYuLi
  • 2013年07月05日 17:29
  • 526

poj 1952 BUY LOW, BUY LOWER dp

点击打开题目链接

POJ 1952 BUY LOW, BUY LOWER DP

BUY LOW , BUY LOWER Time Limit: 1000MS Memory Limit: 30000KB http://poj.org/problem?id=1952 先来看看d...

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

/* THE PROGRAM IS MADE BY PYY */ /*-----------------------------------------------------------------...

POJ 1952 BUY LOW, BUY LOWER

题目大意:         “低价买入”几乎成了牛市大赚的准则,但作为一个更资深的投资者你应该遵循“低价买入,更低价买入”的准则,你每次买一支股票,都要比前一次买入的股票价格要低,这样买的次数越多越好...

POJ1952 BUY LOW, BUY LOWER

这题要求最长下降子序列的长度和个数,我们可以增加 数组maxlen[size](记录当前第1个点到第i个点之间的最长下降序列长度) 和maxnum[size](记录1~i之间的最长下降序列个数 ) ,...

poj 1952 BUY LOW, BUY LOWER【解法一】

dp统计方案数+离散化
  • sdfzyhx
  • sdfzyhx
  • 2016年07月28日 20:47
  • 199

POJ-1952 BUY LOW, BUY LOWER(线性DP)

POJ-1952 BUY LOW, BUY LOWER(线性DP)
  • Dacc123
  • Dacc123
  • 2016年01月04日 19:42
  • 184
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 1952 BUY LOW, BUY LOWER
举报原因:
原因补充:

(最多只允许输入30个字)