poj1952 BUY LOW, BUY LOWER【线性DP】【输出方案数】
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions:11148 | Accepted: 3920 |
Description
"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
* Lines 2..etc: A series of N space-separated integers, ten per line except the final line which might have fewer integers.
Output
* 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】【输出方案数】的更多相关文章
- POJ-1952 BUY LOW, BUY LOWER(线性DP)
BUY LOW, BUY LOWER Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9244 Accepted: 3226 De ...
- [POJ1952]BUY LOW, BUY LOWER
题目描述 Description The advice to "buy low" is half the formula to success in the bovine stoc ...
- USACO Section 4.3 Buy low,Buy lower(LIS)
第一眼看到题目,感觉水水的,不就是最长下降子序列嘛!然后写……就呵呵了..要判重,还要高精度……判重我是在计算中加入各种判断.这道题比看上去麻烦一点,但其实还好吧.. #include<cstd ...
- 洛谷P2687 [USACO4.3]逢低吸纳Buy Low, Buy Lower
P2687 [USACO4.3]逢低吸纳Buy Low, Buy Lower 题目描述 “逢低吸纳”是炒股的一条成功秘诀.如果你想成为一个成功的投资者,就要遵守这条秘诀: "逢低吸纳,越低越 ...
- Buy Low, Buy Lower
Buy Low, Buy Lower 给出一个长度为N序列\(\{a_i\}\),询问最长的严格下降子序列,以及这样的序列的个数,\(1 <= N <= 5000\). 解 显然我们可以很 ...
- 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 ...
- POJ 1952 BUY LOW, BUY LOWER 动态规划题解
Description The advice to "buy low" is half the formula to success in the bovine stock mar ...
- BUY LOW, BUY LOWER_最长下降子序列
Description The advice to "buy low" is half the formula to success in the bovine stock mar ...
- poj1952 BUY LOW, BUY LOWER[线性DP(统计不重复LIS方案)]
如题.$N \leqslant 5000$. 感觉自己思路永远都是弯弯绕绕的..即使会做也会被做繁掉..果然还是我太菜了. 递减不爽,先倒序输入算了.第一问做个LIS没什么说的.第二问统计个数,考虑什 ...
随机推荐
- nodejs基础 -- NPM 使用介绍
npm:是nodejs的包管理工具,随NodeJS一起安装的,能解决NodeJS代码部署上的很多问题,如: 1.允许用户从NPM服务器下载别人编写的第三方包到本地使用. 2.允许用户从NPM服务器下载 ...
- jQuery中 wrap() wrapAll() 与 wrapInner()的差异
wrap() 方法把每个被选元素放置在指定的 HTML 内容或元素中.jQuery 文档操作 - wrapAll() 方法 wrapAll() 在指定的 HTML 内容或元素中放置所有被选的元素.jQ ...
- js中页面跳转(href)中文参数传输方式
编码: escape(参数); 解码: unescape(参数);
- React封装RadioGroup
class RadioGroup extends React.Component { getRadioComponent(item, index) { return <div className ...
- OpenCV学习:OpenCV文件一览
了解一些OpenCV代码整体的模块结构后,再重点学习自己感兴趣的部分,会有一种一览众山小的感觉~ Come on! C:\OpenCV\opencv\build\include文件夹下包含两个文件夹: ...
- 阿里巴巴CI/CD之分层自动化
一佛是阿里巴巴B2B事业群高级产品经理.从事多年互联网系统的研发和测试工作,目前主要负责云效分层自动化测试的产品设计.因为自动化测试在实践过程中,总是碰到各种各样的问题,导致进入自动化测试盲区.所以, ...
- PHP开启伪静态配置
1.检测Apache是否开启mod_rewrite功能 可以通过php提供的phpinfo()函数查看环境配置,找到“Loaded Modules”,其中列出了所有apache2handler已经开启 ...
- 如何创建 SVN 服务器,并搭建自己的 SVN 仓库
SVN是一个广泛使用的版本控制系统,在日常工作中有广泛的应用空间,比如文档管理,我们就可以搭建一个 SVN 仓库,日常需要归类保存的文档统统上传到仓库中,那如何创建 SVN 服务器, 又如何搭建 SV ...
- 2.1 C语言下的位运算
位运算符: 注:运算量仅仅能为整型和字符型数据,不能是实数型的数据. 当进行&运算时:0&1=0.1&0=0:1&1=1:0&0=0: 当进行|运算时:0|1= ...
- SDUT -refresh的停车场(栈和队列)
题目描写叙述 refresh近期发了一笔横財,开了一家停车场.因为土地有限,停车场内停车数量有限,可是要求进停车场的车辆过多. 当停车场满时,要进入的车辆会进入便道等待.最先进入便道的车辆会优先 进 ...