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. java基础-迭代器(Iterator)与增强for循环

    java基础-迭代器(Iterator)与增强for循环 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Iterator迭代器概述 Java中提供了很多个集合,它们在存储元素时 ...

  2. day4 数组学习

    java提供的数组排序操作:java.util.Arrays.sort(数组名): java提供的数组复制:system.arraycopy(源数组名称,源数组开始点下标,目标数组名称,目标数组开始下 ...

  3. spring框架学习(八)spring管理事务方式之注解配置

    1.DAO AccountDao.java package cn.mf.dao; public interface AccountDao { //加钱 void increaseMoney(Integ ...

  4. 使用JavaScript 修改浏览器 URL 地址栏

    现在的浏览器里,有一个十分有趣的功能,你可以在不刷新页面的情况下修改浏览器URL;在浏览过程中.你可以将浏览历史储存起来,当你在浏览器点击后退按钮的时候,你可以冲浏览历史上获得回退的信息,这听起来并不 ...

  5. 20155336 2016-2017-2《JAVA程序设计》第七周学习总结

    20155336 2016-2017-2<JAVA程序设计>第七周学习总结 教材学习内容总结 第十三章 认识时间与日期 格林威治标准时间:简称GMT时间,参考格林威治皇家天文台的标准太阳时 ...

  6. 20155307 2016-2017-2 《Java程序设计》第5周学习总结

    20155307 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 这两章主要讲的是如何处理程序中的异常情况,对于错误,java会将其打包成对象,可以用&quo ...

  7. css3 加载动画效果

    Loading 动画效果一           HTML 代码: <div class="spinner"> <div class="rect1&quo ...

  8. numpy_array与PIL.Image之间的互转

    # conding:utf-8 import matplotlib.pyplot as plt import numpy as np import PIL.Image as image # 图片的读取 ...

  9. python初步学习-python模块之 commands

    commands 通过 os.popen() 执行 shell 命令,返回两个对象,一个是 状态码(Int).另一个为命令输出(str) commands.getoutput(cmd) 返回命令执行输 ...

  10. javascript 中的类数组和数组

    什么是类数组呢? 我们先来看一段代码: function fn() { console.dir(arguments); } fn(1,2,3,4,5,6,7,8,9,10); 这段代码的执行后,在 c ...