BUY LOW, BUY LOWER
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions:11148   Accepted: 3920

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

题意:

其实就是一个最长下降子序列。并且要求输出方案数。

思路:

最长下降子序列好做 dp[i]表示以i为结尾的子串的最长下降子序列长度。

难点是方案数。如果可以考虑重复的子序列的话也比较简单,如果i是由j转移来的,方案数加上j的方案数就可以了。

但是题目中需要考虑去掉重复的个数。

如果在前面找到了某个数和当前的i的stock是相同的话,就不用再重复算前面的了。比如一个k < j < i,stock[j] = stock[i],k可以转移到j和i。如果 j~i之间有一个数m,可以转移到i,那么stock[m] > stock[i],且由于stock[j] = stock[i],stock[k] > stock[j],所以一定有stock[k] > stock[m]

啊啊啊啊啊编不下去了,想不通。

好了打电话给zcx已经经过点拨想通了。真开心。

我们假设j < i并且stock[j] = stock[i],并假设dp[i]就是这个最大的答案。如果dp[i] > dp[j], 那么说明,j+1~i-1中一定还有数在这个子序列中,dp[i]是由j+1~i-1中的某个数转移来的。dp[i]不可能小于dp[j], 最多是相等。当他们相等时,统计答案的时候分别加上就行了,也就是53行到57行的作用。

【日常感谢zcx大恩大德】

 //#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<map> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; int n;
const int maxn = ;
int stock[maxn], sum[maxn];
int dp[maxn]; int main()
{
while(scanf("%d", &n) != EOF){
for(int i = ; i <= n; i++){
scanf("%d", &stock[i]); } //memset(dp, -inf, sizeof(dp));
stock[] = inf;
int cnt = ;
for(int i = ; i <= n; i++){
for(int j = ; j < i; j++){
if(stock[j] > stock[i]){
dp[i] = max(dp[i], dp[j] + );
}
}
} sum[] = ;
for(int i = ; i <= n; i++){
for(int j = i - ; j >= ; j--){
if(stock[i] < stock[j] && dp[j] + == dp[i]){
sum[i] += sum[j];
}
if(stock[i] == stock[j])break;
}
} int ans = , anssum = ;
for(int i = ; i <= n; i++){
ans = max(dp[i], ans);
}
for(int i = ; i <= n; i++){
if(dp[i] == ans){
anssum += sum[i];
}
} printf("%d %d\n", ans, anssum);
} return ;
}

poj1952 BUY LOW, BUY LOWER【线性DP】【输出方案数】的更多相关文章

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

    BUY LOW, BUY LOWER Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9244 Accepted: 3226 De ...

  2. [POJ1952]BUY LOW, BUY LOWER

    题目描述 Description The advice to "buy low" is half the formula to success in the bovine stoc ...

  3. USACO Section 4.3 Buy low,Buy lower(LIS)

    第一眼看到题目,感觉水水的,不就是最长下降子序列嘛!然后写……就呵呵了..要判重,还要高精度……判重我是在计算中加入各种判断.这道题比看上去麻烦一点,但其实还好吧.. #include<cstd ...

  4. 洛谷P2687 [USACO4.3]逢低吸纳Buy Low, Buy Lower

    P2687 [USACO4.3]逢低吸纳Buy Low, Buy Lower 题目描述 “逢低吸纳”是炒股的一条成功秘诀.如果你想成为一个成功的投资者,就要遵守这条秘诀: "逢低吸纳,越低越 ...

  5. Buy Low, Buy Lower

    Buy Low, Buy Lower 给出一个长度为N序列\(\{a_i\}\),询问最长的严格下降子序列,以及这样的序列的个数,\(1 <= N <= 5000\). 解 显然我们可以很 ...

  6. USACO 4.3 Buy Low, Buy Lower

    Buy Low, Buy Lower The advice to "buy low" is half the formula to success in the stock mar ...

  7. POJ 1952 BUY LOW, BUY LOWER 动态规划题解

    Description The advice to "buy low" is half the formula to success in the bovine stock mar ...

  8. BUY LOW, BUY LOWER_最长下降子序列

    Description The advice to "buy low" is half the formula to success in the bovine stock mar ...

  9. poj1952 BUY LOW, BUY LOWER[线性DP(统计不重复LIS方案)]

    如题.$N \leqslant 5000$. 感觉自己思路永远都是弯弯绕绕的..即使会做也会被做繁掉..果然还是我太菜了. 递减不爽,先倒序输入算了.第一问做个LIS没什么说的.第二问统计个数,考虑什 ...

随机推荐

  1. BCM_I2C函数更改

    版本:sdk-xgs-robo- 平台:BCM53344 应用:控制POE芯片 描述:POE控制芯片使用PD69200,使用i2c与其通信,每次需要发送15字节数据,并接受15字节的返回数据. 1.更 ...

  2. smb使用 ------转载自http://blog.csdn.net/tlaff/article/details/5463068

    一.在Linux系统中查看网络中Windows共享文件及Linux中的Samba共享文件: 常用到smbclient:用法如下 [root@localhost ~]# smbclient  -L  / ...

  3. MindManager篇

    MindManager:新建脑图 MindManager:大纲视图(批阅文档结构) MindManager:导出为其他格式 MindManager:插入基本插入主题.备注,标记等) MindManag ...

  4. Unity战斗系统之AI自主决策

    http://www.taikr.com/course/448/tasks http://www.xuanyusong.com/archives/1840 http://www.cnblogs.com ...

  5. 关于Java 枚举类型的自定义属性

    package com.cpic.test;/** * 关于枚举类型自定义属性 * */public enum Provious { ANHUI("皖", 1),BAIJING(& ...

  6. web开发中的安全问题

    web开发中很多东西由前段来负责判断,比如常见的邮箱 电话号码,前端判断到不是一个正确的格式,在你点击提交时候提示你格式填错了,然后不请求后端php,直到你填写正确的格式为止.这种其实可以修改js或者 ...

  7. Credential

    https://www.cnblogs.com/Hawk-Hong/p/4293651.html 在项目开发,我们经常会使用WebService,但在使用WebService时我们经常会考虑以下问题: ...

  8. Sass基础——Rem与Px的转换

    rem是CSS3中新增加的一个单位值,他和em单位一样,都是一个相对单位.不同的是em是相对于元素的父元素的font-size进行计算:rem是相对于根元素html的font-size进行计算.这样一 ...

  9. oracle_存储过程_有参数_获取部门装置层级树

    create or replace procedure P_UTIL_TREE(P_APPL_NAME in VARCHAR2, P_HIERARCHY_TYP in VARCHAR2, TREETY ...

  10. Java类的设计----Object 类

    Object类 Object类是所有Java类的根父类如果在类的声明中未使用extends关键字指明其父类,则默认父类为Object类 public class Person { ... } 等价于: ...