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 逢低吸纳详细解题报告
问题描述: "逢低吸纳"是炒股的一条成功秘诀.如果你想成为一个成功的投资者,就要遵守这条秘诀: "逢低吸纳,越低越买" 这句话的意思是:每次你购买股票时的股 ...
随机推荐
- np.repeat函数
np.repeat用法 觉得有用的话,欢迎一起讨论相互学习~Follow Me np.repeat用于将numpy数组重复 一维数组重复三次 import numpy as np # 随机生成[0,5 ...
- 51 nod 1243 排船的问题
1243 排船的问题http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1243 题目来源: Codility 基准时间限制:1 ...
- springboot 以jar方式在linux后台运行
linux命令如下: nohup java -jar 自己的springboot项目.jar >日志文件名.log 2>&1 & 命令解释: nohup:不挂断地运行命令, ...
- Java并发编程原理与实战一:聊聊并发
一.大纲 •你真的了解并发吗 •多线程和并发 •多线程和多进程 •线程一定快吗 •学习并发的四个阶段 •学习目标 •适合人群 •荐书 二.学习并发的四个阶段 •熟练掌握API,能够完成并发编程 • ...
- Linux常用的20个命令
以下为20个命令 1.ls命令:ls命令式列出目录内容(List Directory Contents)的意思.运行它就是列出文件夹里面的内容,可能是文件也可能是文件夹. root@tecmint:~ ...
- 通过微信公众号ID生成公众号的二维码
username为公众号id http://open.weixin.qq.com/qr/code/?username=wyjiaolian
- LintCode 58: Compare Strings
LintCode 58: Compare Strings 题目描述 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是大写字母. 样例 给出A = "ABCD&q ...
- JavaScript逻辑运算符
逻辑运算符 或与非:&& || ! ----------------------------------------------------------- ...
- Hive笔记之导出查询结果
一.导出到本地 导出查询结果到本地: INSERT OVERWRITE LOCAL DIRECTORY "/tmp/hive-result/t_visit_video" SELEC ...
- JS设计模式——11.适配器模式
适配器模式概述 适配器模式可用来在现有接口和不兼容的类之间进行适配.使用这种模式的对象又叫包装器(wrapper). 适配器特点 从表面看,适配器模式很像门面模式.她们都要对别的对象进行包装并改变其呈 ...