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;
}



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

Usaco 4.3.1 Buy Low, Buy Lower 详细解题报告

Usaco 4.3.1 Buy Low, Buy Lower By 小兔齐齐 描述 “逢低吸纳”是炒股的一条成功秘诀。如果你想成为一个成功的投资者,就要遵守这条秘诀: "逢低吸纳,越...
  • dingyaguang117
  • dingyaguang117
  • 2010年08月25日 11:30
  • 2658

BUY LOW, BUY LOWER----POJ_1952----最长递减子序列

题目地址:http://poj.org/problem?id=1952 BUY LOW, BUY LOWER Time Limit: 1000MS   Memory L...
  • dr5459
  • dr5459
  • 2012年07月20日 08:57
  • 1358

usaco training 4.3.1 Buy Low, Buy Lower 题解

Buy Low, Buy Lower题解 The advice to "buy low" is half the formula to success in the stock market. B...
  • u013724185
  • u013724185
  • 2014年02月27日 08:43
  • 1190

poj 1952 BUY LOW, BUY LOWER dp 但去重真是令人纠结啊!!!

这道题一开始看感觉很简单,就一个降序问题,后来这个去重真是让我煞费苦心啊!!!注意b[]数组的作用  #include using namespace std; int dp[5005];//记...
  • youngyangyang04
  • youngyangyang04
  • 2011年08月20日 13:52
  • 607

POJ1952 BUY LOW, BUY LOWER

这题要求最长下降子序列的长度和个数,我们可以增加 数组maxlen[size](记录当前第1个点到第i个点之间的最长下降序列长度) 和maxnum[size](记录1~i之间的最长下降序列个数 ) ,...
  • zhang360896270
  • zhang360896270
  • 2011年08月19日 14:22
  • 2806

【USACO 2002 February】BUY LOW,BUY LOWER 低价购买

【USACO 2002 February】BUY LOW,BUY LOWER 低价购买Description  “低价购买”这条建议是在奶牛股票市场取得成功的一半规则。要想被认为是伟大的投资者,你必须...
  • codingdd
  • codingdd
  • 2017年03月09日 09:33
  • 208

poj 1952 BUY LOW, BUY LOWER dp

点击打开题目链接
  • cstdio_hao
  • cstdio_hao
  • 2014年11月03日 20:25
  • 261

POJ 1952 BUY LOW, BUY LOWER DP

BUY LOW , BUY LOWER Time Limit: 1000MS Memory Limit: 30000KB http://poj.org/problem?id=1952 先来看看d...
  • Stitch_11752
  • Stitch_11752
  • 2016年06月28日 22:05
  • 162

POJ 1952 Buy Low,Buy Lower

求最长下降子序列及其个数(严格的…..)
  • neighthorn
  • neighthorn
  • 2016年07月13日 20:03
  • 249

POJ 1952 C++:BUY LOW, BUY LOWER

思路: 1. 用两个数组分别存长度和方法数(动态规划) 2. 找到第一个比i大的数,将已i结尾的长度和方法数赋值 3. 如果又找到一个比i大的数,长度正好为i-1,那么就更新i的方法数 4. ...
  • u010203544
  • u010203544
  • 2017年01月03日 15:19
  • 143
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 1952 BUY LOW, BUY LOWER
举报原因:
原因补充:

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