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. 基于mvcpager的分页(get请求,刷新页面),提供两种样式(来自bootstrap的样式)

    使用方法:先把mvcpager.dll引用加入mvc项目 下载路径在本文末尾 前台代码 前台: @{ Layout = null; } @using Webdiyer.WebControls.Mvc ...

  2. hbase集群部分节点HRegionServer启动后自动关闭的问题

    参考链接 http://f.dataguru.cn/thread-209058-1-1.html 我有4HRegionServer节点,1个master,其中3个是unbuntu 系统,2个节点是ce ...

  3. 使用Visual Studio将C#生成DLL文件的方法

    1.命令方式 打开Visual Studio安装目录下的开发人员命令提示 译 File.cs 以产生 File.exe csc File.cs 编译 File.cs 以产生 File.dll csc ...

  4. 帝国CMS当前位置中的“首页”二字如何修改

    1.帝国CMS当前位置首页那两个字在哪里可以修改吗? 2.[!--newsnav--]该处的首页链接,请问在哪儿修改? 3.导航条[!--newsnav--]默认首页为:“首页”可以更改么? 4.导航 ...

  5. HTML的设计与应用

    <html> <head><!-- 设置网页头标题--> <!-- 不需要在页面中显示的内容写在这个里面 --> <base href=" ...

  6. python2.0_day22_web聊天室二

    上节内容已经实现了客户端使用长轮询的方式获取消息的功能.但是还没有展现到前端.本节内容将实现1.展现消息到前端窗口.2.客户端之间发送图片和文件.3.文件上传时显示进度条 下面我们来实现上面3个功能. ...

  7. 当singleton Bean依赖propotype Bean,可以使用在配置Bean添加look-method来解决

    在Spring里面,当一个singleton bean依赖一个prototype bean,因为singleton bean是单例的,因此prototype bean在singleton bean里面 ...

  8. mongoDB在windows64上安装

    1.下载64位:mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi 2.安装目录:将应用安装到此目录下面:C:\MongoDB\ 3 ...

  9. string permutation with upcase and lowcase

    Give a string, which only contains a-z. List all the permutation of upcase and lowcase. For example, ...

  10. C++Primer 异常处理

    异常是指存在于运行时的反常行为,这些行为超出了函数正常功能的范围. 检测出问题的部分发出某信号表明程序遇到了故障,而且信号的发出方无须知道故障将在何处得到解决. 异常处理机制为程序中异常检测和异常处理 ...