hdu 3401 单调队列优化+dp
http://acm.hdu.edu.cn/showproblem.php?pid=3401
Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5188 Accepted Submission(s): 1776
He
forecasts the next T days' stock market. On the i'th day, you can buy
one stock with the price APi or sell one stock to get BPi.
There are some other limits, one can buy at most ASi stocks on the i'th day and at most sell BSi stocks.
Two
trading days should have a interval of more than W days. That is to
say, suppose you traded (any buy or sell stocks is regarded as a
trade)on the i'th day, the next trading day must be on the (i+W+1)th day
or later.
What's more, one can own no more than MaxP stocks at any time.
Before
the first day, lxhgww already has infinitely money but no stocks, of
course he wants to earn as much money as possible from the stock market.
So the question comes, how much at most can he earn?
The first line of each case are three integers T , MaxP , W .
(0 <= W < T <= 2000, 1 <= MaxP <= 2000) .
The
next T lines each has four integers APi,BPi,ASi,BSi(
1<=BPi<=APi<=1000,1<=ASi,BSi<=MaxP), which are mentioned
above.
5 2 0
2 1 1 1
2 1 1 1
3 2 1 1
4 3 1 1
5 4 1 1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define inf 120
#define LL long long
inline int read(int f = )
{
char c = getchar();while (!isdigit(c)) { if (c == '-')f = -; c = getchar(); }
int r = ; while (isdigit(c)) { r = r * + c - '';c = getchar(); } return r*f;
}
int f[][];
int ap[], bp[], as[], bs[];
struct node2 { int w, k; };
deque<node2>q;
int main()
{
int i, j, k, cas, T, MAX_P, W;
cin >> cas;
while (cas--) {
T = read();
MAX_P = read();
W = read();
for (i = ;i <= T;++i)
{
ap[i] = read();
bp[i] = read();
as[i] = read();
bs[i] = read();
}
memset(f, -inf, sizeof(f));
for (int i = ; i <= W + ; i++) {//第一天到W+1天只都是只能买的
for (int j = ; j <= min(MAX_P, as[i]); j++) {
f[i][j] = -ap[i] * j;
}
}
f[][] = ;
for (i = ;i <= T;++i)
{
int u = i - W - ;
for (j = ;j <= MAX_P;++j)
{
f[i][j] = max(f[i][j],f[i - ][j]);
if (u < )continue;
while (!q.empty() && q.back().w < f[u][j] + j*ap[i])q.pop_back();
q.push_back(node2{f[u][j]+j*ap[i],j});
while (!q.empty() && j - q.front().k > as[i])q.pop_front();
if (!q.empty()) f[i][j] = max(f[i][j], q.front().w - j*ap[i]);
}
if (u < )continue;
q.clear();
for (j = MAX_P;j >= ;--j)
{
while (!q.empty() && q.back().w < f[u][j] + j*bp[i])q.pop_back();
q.push_back(node2{f[u][j]+j*bp[i],j});
while (!q.empty() && q.front().k -j> bs[i])q.pop_front();
if (!q.empty()) f[i][j] = max(f[i][j], q.front().w - j*bp[i]);
}
q.clear();
}
int ans = ;
for (i = ;i <= MAX_P;++i)
ans = max(ans,f[T][i]);
printf("%d\n", ans);
}
return ;
}
hdu 3401 单调队列优化+dp的更多相关文章
- hdu 3401 单调队列优化DP
Trade Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- hdu 3401 单调队列优化动态规划
思路: 动态方程很容易想到dp[i][j]=max(dp[i][j],dp[i-w-1][j-k]-k*ap[i],dp[i-w-1][j+k]+k*bp[i]): dp[i][j]表示第i天拥有j个 ...
- 【单调队列优化dp】HDU 3401 Trade
http://acm.hdu.edu.cn/showproblem.php?pid=3401 [题意] 知道之后n天的股票买卖价格(api,bpi),以及每天股票买卖数量上限(asi,bsi),问他最 ...
- bzoj1855: [Scoi2010]股票交易 单调队列优化dp ||HDU 3401
这道题就是典型的单调队列优化dp了 很明显状态转移的方式有三种 1.前一天不买不卖: dp[i][j]=max(dp[i-1][j],dp[i][j]) 2.前i-W-1天买进一些股: dp[i][j ...
- 单调队列优化DP,多重背包
单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...
- 单调队列优化DP——习题收集
前言 感觉可以用单调队列优化dp的模型还是挺活的,开个随笔记录一些遇到的比较有代表性的模型,断续更新.主要做一个收集整理总结工作. 记录 0x01 POJ - 1821 Fence,比较适合入门的题, ...
- Parade(单调队列优化dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others) ...
- bzoj1855: [Scoi2010]股票交易--单调队列优化DP
单调队列优化DP的模板题 不难列出DP方程: 对于买入的情况 由于dp[i][j]=max{dp[i-w-1][k]+k*Ap[i]-j*Ap[i]} AP[i]*j是固定的,在队列中维护dp[i-w ...
- hdu3401:单调队列优化dp
第一个单调队列优化dp 写了半天,最后初始化搞错了还一直wa.. 题目大意: 炒股,总共 t 天,每天可以买入na[i]股,卖出nb[i]股,价钱分别为pa[i]和pb[i],最大同时拥有p股 且一次 ...
随机推荐
- SPDY
转载SPDY 是什么 SPDY 是 Google 开发的基于传输控制协议 (TCP) 的应用层协议 ,开发组正在推动 SPDY 成为正式标准(现为互联网草案).SPDY 协议旨在通过压缩.多路复用和优 ...
- (转)使用ServiceStack构建Web服务
提到构建WebService服务,大家肯定第一个想到的是使用WCF,因为简单快捷嘛.首先要说明的是,本人对WCF不太了解,但是想快速建立一个WebService,于是看到了MSDN上的这一篇文章 Bu ...
- python基础25 -----python高级用法
一.Event 1.为什么会有Event? 线程的一个关键特性就是每个线程的运行都是独立运行且状态不可预测.如果程序中的线程需要通过别的线程的状态来判断自己线程中的 某个程序是否需要执行,那么Even ...
- cqlsh script
1.time类型 cqlsh> COPY my_keyspace.typetest from STDIN;Using 1 child processes Starting copy of my_ ...
- Django为什么要跳转到不同的页面来实现不同的功能
其实是不同将信息提交给不同的页面交给不同的页面去处理同一个数据库,不同的模块实现不同的功能,当要实现某一个功能的时候直接跳转到那一个功能下面的url,可以把要实现的功能区分开,以python面向对象的 ...
- Android 结束进程的方法forceStopPackage
ActivityManager sd = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); Method method = Clas ...
- C#将图片白色背景设置为透明
Image image = System.Drawing.Image.FromFile(@"C:\A.JPG"); Bitmap pbitmap = new Bitmap(imag ...
- 如何修改Eclipse中的快捷键
首先打开Eclipse,Windows->Preferences ↓ 进入Preferences界面后,选择General->Keys ↓ 接下来你就会看到: 接下来点击OK就可以生效了.
- 【leetcode刷题笔记】Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 最长公共子序列的C++实现---附二维指针的使用方法
想了挺久到底第一篇在这儿的博客写什么好,刚好这两天又一次看到动态规划的LCS算法觉得还是有点意思的,就拿来写了写,第一篇博客就发它吧. #include<iostream> #includ ...