POJ 1952 BUY LOW, BUY LOWER(最长递减序列,去重, 动态规划)

BUY LOW, BUY LOWER( 传送门)
Time Limit: 1000MS Memory Limit: 30000K

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

Source

ps: 这道题的两个问题都可以用动态规划来求解

1.求最长的递减序列的长度
2.求最长递减序列的种类(不能重复)
第一个问题相对于第二个问题比较简单,一个数求它的递减序列的最大长度,那么它就要用到在它之前,第一个比它的大的数的最大递减序列(最优子结构), 这样一直把问题规模缩小,最小的长度就是它本身,因此我们可以在这个序列之前加上一个无穷大的数,使这个序列的数相对于这个INF都是递减的,方便之后的递推,动态转移方程为:
dp_len[i] = max(dp_len[i], dp_len[j] + 1); 

dp_len[i]表示已a[ i ] 结尾的最长递减序列的长度。

第二个问题:去重是重中之重。比如一个序列5,4,6,4,输出应为2 2,因为只有序列(5,4)和(6,4)符合,注意其中(5,4)出现了两次。我们用dp_cnt[i]来表示以 a[i] 结尾长度为 dp_len[i]的种类。那么dp_cnt[i]应该等于多少呢?

举个粟子:

i123456
a[i]1085952
dp_len[i]123234
dp_cnt[i]111122

可以看出dp_cnt[i] = sum(dp_cnt[j]),  0 <= j < i, dp_len[j] + 1 = dp_len[i], 比如dp_cnt[5] = dp_cnt[2] + dp_cnt[4] = 2;也就是以5结尾的符合序列为(10,8,5)和(10, 9, 5)。

接下来就是去重的选择了,比如对于2结尾的序列(10,8,5,2)出现了两次,那么2的前驱5的选择是任意的吗?答案当然不是,如上表,一个dp_cnt[3] = 1, 另一个dp_cnt[5] = 2,显然后者的选择多些,因此求dp_cnt[i]的过程j的枚举从后面开始,如何判断重复呢?当dp_len[i] == dp_len[j] && a[i] == a[j]时必然重复。

#include<iostream>
#include<cstring>
#include<climits>
#include<algorithm>
using namespace std;
int a[5004];
int dp_len[5004];
int dp_cnt[5004];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    while(cin>>n)
    {
        for(int i = 1; i <= n; i++)cin>>a[i];
        a[0] = INT_MAX;
        memset(dp_len, 0, sizeof dp_len);
        memset(dp_cnt, 0, sizeof dp_cnt);
        int ans_len = -1, ans_cnt = 0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j < i; j++)
            {
                if(a[i] < a[j])
                {
                    dp_len[i] = max(dp_len[j] + 1, dp_len[i]);
                }
            }
            if(ans_len < dp_len[i]){
                ans_len = dp_len[i];
            }
        }
        dp_cnt[0] = 1;
        for(int i = 1; i <= n; i++)
        {
            for(int j = i - 1; j >= 0; j--)
            {
                if(dp_len[i] == dp_len[j] && a[i] == a[j]){
                    break;
                }
                if(dp_len[i] == dp_len[j] + 1 && a[i] < a[j])
                {
                    dp_cnt[i] += dp_cnt[j];
                }
            }
            if(dp_len[i] == ans_len) ans_cnt += dp_cnt[i];
        }
        cout<<ans_len<<" "<<ans_cnt<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值