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 market. But to be considered a great investor you must also follow this problems' advice:
"Buy low, buy lower"
That is, each time you buy a stock, you must purchase more 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 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 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.
By way of example, suppose on successive days stock is selling like this:
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
In the example above, the best investor (by this problem, anyway) can buy at most four times if they purchase at a lower price each time. One four day sequence (there might be others) of acceptable buys is:
Day 2 5 6 10
Price 69 68 64 62
PROGRAM NAME: buylow
INPUT FORMAT
Line 1: | N (1 <= N <= 5000), the number of days for which stock prices are available. |
Line 2..etc: | A series of N positive space-separated integers (which may require more than one line of data) that tell the price for that day. The integers will fit into 32 bits quite nicely. |
SAMPLE INPUT (file buylow.in)
12
68 69 54 64 68 64 70 67
78 62 98 87
OUTPUT FORMAT
Two integers on a single line:
- the length of the longest sequence of decreasing prices
- the number of sequences that have this length
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 OUTPUT (file buylow.out)
4 2 ————————————————————————————————————————————————————————————
这道题乍一看似乎很水
然而……
好吧最长下降子序列可以n^2的求出但是我们还需要序列个数
用加法原理假如遇到a[j]>a[i](j<i) 且f[j]+1==f[i]我们就给计数器d数组d[i]+=d[j]
但这样肯定不是最后的答案,因为每个数的数字需要不一样,而不是下标不一样
用一个next数组记录距离j最近的一个和a[j]相等的数的下标,如果这个下标在i之前我们可以跳过j,如果没有或这个下标在i之后再进行计算,这样的话我们就可以囊括之前的情况并且避免重复计算了
在序列最后加一个0,最终答案就可以很容易的聚到最后一位上了
还有高精qwq
【是时候认认真真写个高精当模板了qwq】
/*
ID:ivorysi
PROG:buylow
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#define inf 0x7fffffff
#define ivorysi
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
using namespace std;
struct bignum {
vector<int> s;
bignum operator =(int x) {
s.clear();
do {
s.push_back(x%);
x/=;
}while(x>);
return *this;
}
bignum operator + (const bignum &b) const {
bignum c;
c.s.clear();
for(int g=,k=;;++k) {
if(g== && k>=b.s.size() && k>=s.size()) {break;}
int x=g;
if(k<b.s.size()) x+=b.s[k];
if(k<s.size()) x+=s[k];
c.s.push_back(x%);
g=x/;
}
return c;
}
bignum &operator +=(const bignum &b) {
*this=*this+b;
return *this;
}
}d[];
ostream& operator << (ostream &out, const bignum& x) {
gongzi(i,x.s.size()-,) {
out<<x.s[i];
}
return out;
}
int n,a[],f[],ans,ans1,next[];
void solve() {
scanf("%d",&n);
siji(i,,n) scanf("%d",&a[i]);
++n;
a[n]=;
siji(i,,n) {f[i]=;d[i]=;}
siji(i,,n) {
siji(j,i+,n) {
if(a[i]>a[j]) {
if(f[i]+>f[j]) {
f[j]=f[i]+;
}
}
}
}
siji(i,,n) {
siji(j,i+,n) {
if(a[i]==a[j]) {next[i]=j;break;}
}
}
siji(i,,n) {
if(f[i]==) d[i]=;
xiaosiji(j,,i) {
if(a[j]>a[i]) {
if(f[j]+==f[i] && (next[j]== || next[j]>i)) {
d[i]=d[i]+d[j];
}
}
}
}
printf("%d ",f[n]-);
cout<<d[n]<<endl;
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("buylow.in","r",stdin);
freopen("buylow.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
USACO 4.3 Buy Low, Buy Lower的更多相关文章
- USACO Section 4.3 Buy low,Buy lower(LIS)
第一眼看到题目,感觉水水的,不就是最长下降子序列嘛!然后写……就呵呵了..要判重,还要高精度……判重我是在计算中加入各种判断.这道题比看上去麻烦一点,但其实还好吧.. #include<cstd ...
- poj1952 BUY LOW, BUY LOWER【线性DP】【输出方案数】
BUY LOW, BUY LOWER Time Limit: 1000MS Memory Limit: 30000K Total Submissions:11148 Accepted: 392 ...
- POJ 1952 BUY LOW, BUY LOWER 动态规划题解
Description The advice to "buy low" is half the formula to success in the bovine stock mar ...
- POJ-1952 BUY LOW, BUY LOWER(线性DP)
BUY LOW, BUY LOWER Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9244 Accepted: 3226 De ...
- 洛谷P2687 [USACO4.3]逢低吸纳Buy Low, Buy Lower
P2687 [USACO4.3]逢低吸纳Buy Low, Buy Lower 题目描述 “逢低吸纳”是炒股的一条成功秘诀.如果你想成为一个成功的投资者,就要遵守这条秘诀: "逢低吸纳,越低越 ...
- [POJ1952]BUY LOW, BUY LOWER
题目描述 Description The advice to "buy low" is half the formula to success in the bovine stoc ...
- Buy Low, Buy Lower
Buy Low, Buy Lower 给出一个长度为N序列\(\{a_i\}\),询问最长的严格下降子序列,以及这样的序列的个数,\(1 <= N <= 5000\). 解 显然我们可以很 ...
- BUY LOW, BUY LOWER_最长下降子序列
Description The advice to "buy low" is half the formula to success in the bovine stock mar ...
- Usaco 4.3.1 Buy Low, Buy Lower 逢低吸纳详细解题报告
问题描述: "逢低吸纳"是炒股的一条成功秘诀.如果你想成为一个成功的投资者,就要遵守这条秘诀: "逢低吸纳,越低越买" 这句话的意思是:每次你购买股票时的股 ...
随机推荐
- 获取异常信息e.printStackTrace()的内容
获取异常信息e.printStackTrace()的内容 最近做项目的时候需要记录操作的日志,但是记录异常信息的是发现使用e.getMessage()根本无法满足需要,并且e.getMessage() ...
- python---基础知识回顾(六)网络编程2(处理粘包)
前戏: 之前在python---基础知识回顾(六)网络编程异步模块中提到过粘包现象,而且在使用twisted中提到过一种处理办法,按行接收lineReceived,当收到\r\n换行符时,才去缓冲区中 ...
- nandflash,norflash,sdram,emmc,rom,ram等各种存储器识别
老是被nandflash,norflash,sdram,emmc,rom,ram搞混,所以在这里总结一下,也为了更好的分清他们之间的关系,以至于别人问的时候不至于说不清. 我们不谈这些名次的由来,只说 ...
- jQuery中使用attribute,prop获取,设置input的checked值
1.prop方法获取.设置checked属性 当input控件checkbox设置了checked属性时,无论checked=”“或 checked=”checked”,$(obj).prop(“ch ...
- JS中的异步与回调
问题的引出:在js中使用异步调用时,有可能会出现在异步的回调函数中设置调用之外的变量值,但在异步调用完成后去使用变量,却发现这些变量值并没有被成功设置的情况.如: google map中的地理编码,地 ...
- Dubbo学习笔记1:使用Zookeeper搭建服务治理中心
Zookeeper是Apache Hadoop的子项目,是一个树形的目录服务,支持变更推送,适合作为Dubbo服务的注册中心,工业强度较高,推荐生成环境使用. , 下面结合上图介绍Zookeeper在 ...
- Golang入门教程(一)GOPATH与工作空间(Windows)
https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/01.2.md Windows 环境: 下面我就 ...
- TV 开发相关
1.设置全屏,隐藏虚拟按键 1.activity oncreate中 @Override 2 protected void onCreate (Bundle savedInstanceState) { ...
- 【leetcode 简单】 第七十六题 移动零
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作, ...
- Java编程思想 4th 第3章 操作符
有了数据,还需要进行数据间的运算,因此Java中也有数据间运算的各种符号,书本称之为操作符,正确的翻译应该是运算符. Java中的运算符同C++相同,运算符同运算符对象构成表达式,表达式是运算对象及运 ...