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

* 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

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

【题意】 求最长下降子序列和长度及个数

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
const int inf=0x7777777;
int dp[N],a[N],num[N];
void get_ans(int n)
{
int ans=;
for(int i=; i<=n; i++)
{
dp[i]=;
num[i]=;
}
for (int i=;i<=n; i++)
{
for (int j=;j<i;j++)
{
if (a[i]<a[j])
{
dp[i]= max(dp[i], dp[j]+);
}
}
}
for (int i=; i<=n; i++)
if (dp[i]==) num[i]=;
for (int i=; i<=n; i++)
{
for (int j=i-;j>; j--)
{
if (a[j] > a[i])
{
if (dp[j]+ == dp[i])//dp[i]=dp[j]+1的话,两者位于同一个下降子序列,num[i]加上num[j];
{
num[i] += num[j];
}
}
if (a[j]==a[i])
{
if (dp[i]==) num[i]=;//如果搜索到一个相同的数后仍没有找到符合要求的序列,则为了避免重复赋值为0 break;
}
} }
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
}
get_ans(n);
int w=;
for(int i=;i<=n;i++)
{
if(dp[i]>w) w=dp[i];
}
int cnt=; for(int i=; i<=n; i++)
{
if(dp[i]==w)
{
cnt+=num[i];
}
}
printf("%d %d\n",w,cnt);
}
return ;
}

BUY LOW, BUY LOWER_最长下降子序列的更多相关文章

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

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

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

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

  3. 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 mar ...

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

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

  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. POJ 1952 BUY LOW, BUY LOWER 动态规划题解

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

  9. 最长下降子序列O(n^2)及O(n*log(n))解法

    求最长下降子序列和LIS基本思路是完全一样的,都是很经典的DP题目. 问题大都类似于 有一个序列 a1,a2,a3...ak..an,求其最长下降子序列(或者求其最长不下降子序列)的长度. 以最长下降 ...

随机推荐

  1. hdu---(5038)Grade(胡搞)

    Grade Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Sub ...

  2. 更准确的mysql全文索引

    MYSQL自带的全文索引在查找数据的时候,有非常多的限制,如字符少于3个不能搜索,常用字不能搜索 但mysql 的like进行查询的时候又非常的慢 但你需要用到比较准确的查询的时候,要么不用mysql ...

  3. lrzsz在CentOS7的安装

    在超级用户下打一句命令: yum install lrzsz 或者,在普通用户打一句命令,需要输入超级用户密码: sudo yum install lrzsz 然后使用Xshell 5建立连接即可

  4. SpringMVC 配置定时执行任务

    1.在SpringMVC配置文件中添加 xmlns:task="http://www.springframework.org/schema/task" http://www.spr ...

  5. android:exported

    这个属性用于指示该服务是否能够被其他应用程序组件调用或跟它交互.如果设置为true,则能够被调用或交互,否则不能.设置为false时,只有同一个应用程序的组件或带有相同用户ID的应用程序才能启动或绑定 ...

  6. hdu5884 Sort(二分+k叉哈夫曼树)

    题目链接:hdu5884 Sort 题意:n个有序序列的归并排序.每次可以选择不超过k个序列进行合并,合并代价为这些序列的长度和.总的合并代价不能超过T, 问k最小是多少. 题解:先二分k,然后在k给 ...

  7. java配置数据库连接池的方法步骤

    java配置数据库连接池的方法步骤 java配置数据库连接池的方法步骤,需要的朋友可以参考一下   先来了解下什么是数据库连接池数据库连接池技术的思想非常简单,将数据库连接作为对象存储在一个Vecto ...

  8. 安卓手机修改host

    电脑修改 注意:usb设置为调试模式 1.手机必须先root,小米可以安卓开发版系统即可 2.安卓 adb工具(android debug bridge) 3.依次执行下面的命令 1.adb root ...

  9. 查看Linux硬件配置信息

    在网上找了N久,发现了一篇不错的文档,转载一下: 1.查看机器所有硬件信息: dmidecode |more dmesg |more 这2个命令出来的信息都非常多,所以建议后面使用"|mor ...

  10. struts2视频学习笔记 28(OGNL表达式)

    课时28 OGNL表达式 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. Struts 2框架使用OGNL作为默认的表达式语 ...