天天看點

POJ 1952 BUY LOW, BUY LOWER

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