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

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

Source

题意:

其实就是一个最长下降子序列。并且要求输出方案数。

思路:

最长下降子序列好做 dp[i]表示以i为结尾的子串的最长下降子序列长度。

难点是方案数。如果可以考虑重复的子序列的话也比较简单,如果i是由j转移来的,方案数加上j的方案数就可以了。

但是题目中需要考虑去掉重复的个数。

如果在前面找到了某个数和当前的i的stock是相同的话,就不用再重复算前面的了。比如一个k < j < i,stock[j] = stock[i],k可以转移到j和i。如果 j~i之间有一个数m,可以转移到i,那么stock[m] > stock[i],且由于stock[j] = stock[i],stock[k] > stock[j],所以一定有stock[k] > stock[m]

啊啊啊啊啊编不下去了,想不通。

好了打电话给zcx已经经过点拨想通了。真开心。

我们假设j < i并且stock[j] = stock[i],并假设dp[i]就是这个最大的答案。如果dp[i] > dp[j], 那么说明,j+1~i-1中一定还有数在这个子序列中,dp[i]是由j+1~i-1中的某个数转移来的。dp[i]不可能小于dp[j], 最多是相等。当他们相等时,统计答案的时候分别加上就行了,也就是53行到57行的作用。

【日常感谢zcx大恩大德】

 //#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<map> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; int n;
const int maxn = ;
int stock[maxn], sum[maxn];
int dp[maxn]; int main()
{
while(scanf("%d", &n) != EOF){
for(int i = ; i <= n; i++){
scanf("%d", &stock[i]); } //memset(dp, -inf, sizeof(dp));
stock[] = inf;
int cnt = ;
for(int i = ; i <= n; i++){
for(int j = ; j < i; j++){
if(stock[j] > stock[i]){
dp[i] = max(dp[i], dp[j] + );
}
}
} sum[] = ;
for(int i = ; i <= n; i++){
for(int j = i - ; j >= ; j--){
if(stock[i] < stock[j] && dp[j] + == dp[i]){
sum[i] += sum[j];
}
if(stock[i] == stock[j])break;
}
} int ans = , anssum = ;
for(int i = ; i <= n; i++){
ans = max(dp[i], ans);
}
for(int i = ; i <= n; i++){
if(dp[i] == ans){
anssum += sum[i];
}
} printf("%d %d\n", ans, anssum);
} return ;
}

poj1952 BUY LOW, BUY LOWER【线性DP】【输出方案数】的更多相关文章

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

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

  2. [POJ1952]BUY LOW, BUY LOWER

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

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

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

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

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

  5. Buy Low, Buy Lower

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

  6. 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 ...

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

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

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

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

  9. poj1952 BUY LOW, BUY LOWER[线性DP(统计不重复LIS方案)]

    如题.$N \leqslant 5000$. 感觉自己思路永远都是弯弯绕绕的..即使会做也会被做繁掉..果然还是我太菜了. 递减不爽,先倒序输入算了.第一问做个LIS没什么说的.第二问统计个数,考虑什 ...

随机推荐

  1. Windows下 flex + bison 小例子

    .下载flex和bison,网址是http://gnuwin32.sourceforge.net/packages/flex.htm 和http://gnuwin32.sourceforge.net/ ...

  2. html学习笔记五

    关于服务端和client的校验问题 上述的表格信息填写后发现,即使有些信息不添,依旧能够提交 所以针对此问题,我们要在client进行数据填写信息的增强型校验(必添单元,必须填写有效信息,否则无法提交 ...

  3. java与c#的语法对比

    1,命名空间与包 C#为了把实现相似功能的类组织在一起,引入了命名空间的概念(namespace) Java中与此对应的东西叫做包(package) 2,类的访问控制方面的不同 C#只有两种:publ ...

  4. 最大子数组之和(N)

    int maxSum(int *array, int n) { ]; ; ; ; i < n; i++) { ) newsum += array[i]; else newsum = array[ ...

  5. mysql数据库中,查看当前支持的字符集有哪些?字符集默认的collation的名字?

    需求描述: mysql数据库支持很多字符集,那么如何查看当前的mysql版本中支持的或者说可用的字符集有什么呢? 操作过程: 1.使用show character set的方式获取当前版本中支持的字符 ...

  6. tiny6410SDK制作NFS文件系统

    1.初次编译内核出现问题 解决方法:将uboot的tools目录 下的mkimage拷贝到/bin/下. 2.开机后终端出现mmc0: error -110 whilst initialising S ...

  7. NHibernate初学三之条件查询(Criteria Queries)与AspNetPager分页实例

    NHibernate除了SQL与HQL两种查询操作外,还有一种就是条件查询Criteria,本文将从网上整理一些Criteria的理论及小实例,最后通过一个结合AspNetPager分页来加深理解,必 ...

  8. Java精选笔记_Java入门

    Java概述 什么是Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言 JavaSE标准版 是为开发普通桌面和商务应用程序提供的解决方案 JavaEE企业版 是为开发企业级应用程序提供的解 ...

  9. swift学习笔记之—自定义函数的规则说明

    原文出自:www.hangge.com  转载请保留原文链接:http://www.hangge.com/blog/cache/detail_517.html 1,无返回值的函数 func test( ...

  10. 【RF库Collections测试】Convert To List

    Name:Convert To ListSource:Collections <test library>Arguments:[ item ]Converts the given `ite ...