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的更多相关文章

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

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

  2. poj1952 BUY LOW, BUY LOWER【线性DP】【输出方案数】

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

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

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

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

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

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

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

  6. [POJ1952]BUY LOW, BUY LOWER

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

  7. Buy Low, Buy Lower

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

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

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

  9. Usaco 4.3.1 Buy Low, Buy Lower 逢低吸纳详细解题报告

    问题描述: "逢低吸纳"是炒股的一条成功秘诀.如果你想成为一个成功的投资者,就要遵守这条秘诀:  "逢低吸纳,越低越买"  这句话的意思是:每次你购买股票时的股 ...

随机推荐

  1. np.isin判断数组元素在另一数组中是否存在

    np.isin用法 觉得有用的话,欢迎一起讨论相互学习~Follow Me np.isin(a,b) 用于判定a中的元素在b中是否出现过,如果出现过返回True,否则返回False,最终结果为一个形状 ...

  2. Nginx记录-Nginx配置

    1. 启动,停止和重新加载Nginx配置 要启动nginx,请运行可执行文件. 当nginx启动后,可以通过使用-s参数调用可执行文件来控制它. 使用以下语法: nginx -s signal 信号( ...

  3. http协议POST请求头content-type主要的四种取值

    介绍: 在此之前对content-type理解很肤浅,因此必须记录下来现在的理解,以便回顾 Content-Type,从名字上可以理解为内容类型,但在互联网上专业术语叫“媒体类型”,即MediaTyp ...

  4. 线搜索(line search)方法

    在机器学习中, 通常需要求某个函数的最值(比如最大似然中需要求的似然的最大值). 线搜索(line search)是求得一个函数\(f(x)\)的最值的两种常用迭代方法之一(另外一个是trust re ...

  5. 【leetcode 简单】 第五十九题 同构字符串

    给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一 ...

  6. Django-Form表单(验证、定制、错误信息、Select)

      Django form 流程 1.创建类,继承form.Form 2.页面根据类的对象自动创建html标签 3.提交,request.POST       封装到类的对象里,obj=UserInf ...

  7. JS中数组那些事~

    今天闲的没事,看了一些关于数组的操作,好久木有用过,很多都已经忘记的差不多了,所以今天花了一个小时写了一些demo,准备备份一下方便以后使用~~~ 下面是一些工作当中,经常用到的数组的方法,小伙伴可以 ...

  8. VM虚拟机上连接usb无反映

    主机的usb连接又是正常的,排除了usb3.0的接口原因后,突然想到了是不是虚拟机的什么服务没有开?进入到控制面板->管理工具->服务,找到 V开头的,发现原来确实是虚拟机有关usb的服务 ...

  9. JS函数的几种用法

    1.正常使用:

  10. ubuntu的PPA

    PPA,表示Personal Package Archives,也就是个人软件包集很多软件包由于各种原因吧,不能进入官方的Ubuntu软件仓库.为了方便Ubuntu用户使用,launchpad.net ...