题目描述 Description

The advice to "buy low" is half the formula to success in the bovine stock market.To be considered a great investor you must also follow this problems' advice:

                    "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 Description

* Line 1: N (1 <= N <= 5000), the number of days for which stock prices are given

* Lines 2..etc: A series of N space-separated integers, ten per line except the final line which might have fewer integers.

输出描述 Output Description

Two integers on a single line: 
* 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

数据范围及提示 Data Size & Hint

 

之前的一些废话:近日诸事不顺。

题解:对于第一问我们xjb做一遍最长下降子序列即可,对于方案数,在转移的时候顺便计算一下,注意如果发现a[i]=a[j],就要把cnt[i]标记成0,来做到去重。

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<queue>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
#define mem(a,b) memset(a,b,sizeof(a))
inline int read()
{int x=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}while(isdigit(c)){x=x*10+c-'0';c=getchar();}return x*f;
}
const int maxn=5010;
int n,a[maxn],dp[maxn],ans,cnt,dp2[maxn];
int main()
{n=read();for(int i=1;i<=n;i++)a[i]=read(),dp[i]=dp2[i]=1;for(int i=2;i<=n;i++)for(int j=1;j<i;j++){if(a[i]<a[j]){if(dp[j]+1>dp[i])dp[i]=dp[j]+1,dp2[i]=dp2[j];else if(dp[j]+1==dp[i])dp2[i]+=dp2[j];}else if(a[i]==a[j])dp2[i]=0;}for(int i=1;i<=n;i++)ans=max(ans,dp[i]);for(int i=1;i<=n;i++)if(ans==dp[i])cnt+=dp2[i];printf("%d %d\n",ans,cnt);return 0;
}

View Code

总结:

转载于:https://www.cnblogs.com/FYH-SSGSS/p/7136981.html

[POJ1952]BUY LOW, BUY LOWER相关推荐

  1. $Poj1952\ $洛谷$1687\ Buy\ Low,Buy\ Lower$ 线性$DP+$方案计数

    Luogu Description 求一个长度为n的序列a的最长下降子序列的长度,以及这个长度的子序列种数,注意相同的几个子序列只能算作一个子序列. n<=5000,a[i]不超过long范围 ...

  2. USCACO Buy Low, Buy Lower

    求最长下降子序列简单,难点就是求序列的个数更难的就说处理重复序列.求个数代码还好理解判断重复就是从那个数字一直往前找如果找到一个和它相等的(假设这两个数字为ab),看看这两个数字之间有没有可以和后面数 ...

  3. POJ 1952 BUY LOW, BUY LOWER

    $dp$. 一开始想了一个$dp$做法,$dp[i][j]$表示前$i$个数字,下降序列长度为$j$的方案数为$dp[i][j]$,这样做需要先离散化然后用树状数组优化,空间复杂度为${n^2}$,时 ...

  4. buy low buy lower——伪思考

    过于追求编程数量,却忽略了最重要的方法过程的思考,结果必将是不会思考,还可能养成不爱思考的习惯. 代码 1 #include<stdio.h> 2 #include<stdlib.h ...

  5. 专题突破二之优先队列、st表——,Running Median,Sequence,Buy Low Sell High,数据备份,超级钢琴,ZQC的手办

    文章目录 Running Median Sequence Buy Low Sell High [APIO/CTSC 2007] 数据备份 [NOI2010] 超级钢琴 「LibreOJ β Round ...

  6. CodeForces - 867E Buy Low Sell High (贪心 +小顶堆)

    https://vjudge.net/problem/CodeForces-867E 题意 一个物品在n天内有n种价格,每天仅能进行买入或卖出或不作为一种操作,可以同时拥有多种物品,问交易后的最大利益 ...

  7. [ CodeForces 865 D ] Buy Low Sell High

    \(\\​\) \(Description\) 给出\(N\)天股票的价钱\(A_1,...,A_N\),每天可以什么都不做,或者买入或卖出\(1\)支股票,分别花出或收入\(A_i\)元,求最大收益 ...

  8. 51nod 2206 低买高卖codeforces867E Buy Low Sell High 贪心+优先队列

    考虑股票市场,一共有n天. 对于第i天,B君知道股票的价格是每单位a[i]元 在每一天,B君可以选择买入一个单位的股票,卖出一个单位的股票,或者什么都不做. 刚开始B君有无穷多的钱,但是没有任何股票. ...

  9. 【转】别人整理的DP大全

    为什么80%的码农都做不了架构师?>>>    动态规划 动态规划 容易: 1018 , 1050 , 1083 , 1088 , 1125 , 1143 , 1157 , 1163 ...

  10. USACO Section 4

    前言 好久没更新这个系列了,最近闲的无聊写一下.有两题搜索懒得写了. P2737 [USACO4.1]麦香牛块Beef McNuggets https://www.luogu.com.cn/probl ...

最新文章

  1. LeetCode中等题之删除链表的中间节点
  2. Nginx安装、配置及使用总结
  3. [云炬创业基础笔记]第二章创业者测试11
  4. 最新 | Python 官方中文文档正式发布!
  5. 开启DELL Raid卡H730P缓存
  6. DeepLearning.AI第一部分第三周、 浅层神经网络(Shallow neural networks)
  7. 独家首发 | 900页阿里文娱技术实战,8大技术栈解析技术全景
  8. TortoiseSVN设置比较工具为BeyondCompare
  9. 2004年9月全国计算机等级考试二级C语言笔试试题及答案
  10. 计算机应用基础都学什么,计算机应用基础学习计划
  11. 输入法兼容 android,搜狗输入法5.1版发布 兼容Android 4.4
  12. seleinum+requets 下载歌曲
  13. .html怎么查看源代码,html的网页源代码怎么查看
  14. SCD-缓慢变化维-拉链表
  15. 内存泄漏的原因及解决方法
  16. 【沧海拾昧】微机原理:存储器系统
  17. 吴裕雄--天生自然 诗经:鹊踏枝·谁道闲情抛弃久
  18. Linux安装配置jdk11
  19. Altium_Designer中英文技术词汇对照
  20. 详细Http状态查询返回 HTTP 状态代码以响应请求

热门文章

  1. pandas 日期比较大小_计算pandas Dataframe中的日期时间差异
  2. list里面的数据按3个字段排序_springboot2.X手册:redis的7种类型100个方法全解析
  3. pytorch tensor_Pytorch之Tensor操作
  4. TServerSocket阻塞模式下Request-Response编程框架
  5. jdk的安装及环境变量的配置
  6. oracle中DMP文件导入导出例子详解
  7. [PHP] - Laravel 5 的 Hello Wold
  8. C#_自动化测试3_controll IE
  9. 基于DotNetNuke的动态窗体支持(一)
  10. SAMBA最简单的配置方法