POJ 1952
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 7748 | Accepted: 2678 |
Description
"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
* Lines 2..etc: A series of N space-separated integers, ten per line except the final line which might have fewer integers.
Output
* 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 <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; int main()
{
int times[];
int dp[];
int num[];
int n;
int ans, time;
while(scanf("%d", &n) != EOF)
{
ans = ;
time = ;
for (int i = ; i < n; i++)
{
cin>>num[i];
dp[i] = ;
times[i] = ;
}
for (int i = ; i < n; i++)
{
for (int j = i - ; j >= ; j--)
{
if (num[j] > num[i])
{
if (dp[j] + > dp[i])
{
dp[i] = dp[j] + ;
times[i] = times[j];
}
else
{
if (dp[j] + == dp[i])
{
times[i] += times[j];
}
}
}
else
{
if (num[i] == num[j])
{
if (dp[i] == )
{
times[i] = ;
}
break;
}
}
}
}
for (int i = ; i < n; i++)
{
ans = max(ans, dp[i]);
}
for (int i = ; i < n; i++)
{
if (ans == dp[i])
{
time += times[i];
}
}
printf("%d %d\n", ans, time);
}
return ;
}
POJ 1952的更多相关文章
- POJ 1952 BUY LOW, BUY LOWER
$dp$. 一开始想了一个$dp$做法,$dp[i][j]$表示前$i$个数字,下降序列长度为$j$的方案数为$dp[i][j]$,这样做需要先离散化然后用树状数组优化,空间复杂度为${n^2}$,时 ...
- 【最长下降子序列的长度和个数】 poj 1952
转自http://blog.csdn.net/zhang360896270/article/details/6701589 这题要求最长下降子序列的长度和个数,我们可以增加数组maxlen[size] ...
- poj 1952 最长公共子序列计数
看代码就懂了 不解释 3 1 1 1 1 2 2 2 1 1 1 3 第一个3 和最后一个 3 只需要一个就够了,,, #include<iostream> #include< ...
- POJ 1952 BUY LOW, BUY LOWER 动态规划题解
Description The advice to "buy low" is half the formula to success in the bovine stock mar ...
- POJ 1952 DP
思路: 这题要求最长下降子序列的长度和个数,我们可以增加 数组maxlen[size](记录当前第1个点到第i个点之间的最长下降序列长度) 和maxnum[size](记录1~i之间的最长下降序列个数 ...
- POJ 1952 BUY LOW, BUY LOWER DP记录数据
最长递减子序列.加记录有多少个最长递减子序列.然后须要去重. 最麻烦的就是去重了. 主要的思路就是:全面出现反复的值,然后还是同样长度的子序列.这里的DP记录的子序列是以当前值为结尾的时候,而且一定选 ...
- A过的题目
1.TreeMap和TreeSet类:A - Language of FatMouse ZOJ1109B - For Fans of Statistics URAL 1613 C - Hardwood ...
- $2019$ 暑期刷题记录1:(算法竞赛DP练习)
$ 2019 $ 暑期刷题记录: $ POJ~1952~~BUY~LOW, BUY~LOWER: $ (复杂度优化) 题目大意:统计可重序列中最长上升子序列的方案数. 题目很直接的说明了所求为 $ L ...
- POJ 2263 Heavy Cargo(ZOJ 1952)
最短路变形或最大生成树变形. 问 目标两地之间能通过的小重量. 用最短路把初始赋为INF.其它为0.然后找 dis[v]=min(dis[u], d); 生成树就是把最大生成树找出来.直到出发和终点能 ...
随机推荐
- python之map,zip,reduce,filter的用法
1.reduce(func,iterable,initial): 参数: - func 可执行函数 - iterable 可迭代对象 - initial 可选,初始参数 功能描述:调用func函数后, ...
- 火狐浏览器不支持location.reload()(以改变页面大小时重新刷新页面为例)
背景:当页面大小改变时需要重新刷新页面,以适应相应的尺寸 解决方法: var url = window.location.href; var parm = parseInt(Math.random() ...
- POJ 2411 Mondriaan's Dream (状压DP,骨牌覆盖,经典)
题意: 用一个2*1的骨牌来覆盖一个n*m的矩形,问有多少种方案?(1<=n,m<=11) 思路: 很经典的题目,如果n和m都是奇数,那么答案为0.同uva11270这道题. 只需要m个b ...
- WPF中,DataGrid最左边多出一行的解决方案
这种情况下,请在DataGrid的属性里加上这个属性: RowHeaderWidth="0" 必须赋值为0,不能不赋值,也不能赋其他值. 问题解决.
- ceisum_加载倾斜摄影模型
osgb转换为3Dtiles格式(使用工具转换) 然后加载到cesium中(加载代码见下,可以控制模型高度) var offset = function(height,tileset) { conso ...
- python_92_面向对象初体验
class dog: def __init__(self,name): self.name=name def bulk(self): print('%s汪汪汪!'%self.name) d1=dog( ...
- java基础—基础语法1
一.标识符
- js完成打印功能
最近在做项目要求实现打印功能,我采用js方式来实现 window.print();会弹出打印对话框,打印的是window.docunemt.body.innerHTML中的内容,可以局部打印,也可以全 ...
- java 第11次作业:你能看懂就说明你理解了——this关键字
this 代表当前对象
- 【差分约束】poj1275Cashier Employment
比较经典的差分约束 Description A supermarket in Tehran is open 24 hours a day every day and needs a number of ...