Trade

Time Limit: 1000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 3401
64-bit integer IO format: %I64d      Java class name: Main

 
Recently, lxhgww is addicted to stock, he finds some regular patterns after a few days' study.
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?

 

Input

The first line is an integer t, the case number.
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.

 

Output

The most money lxhgww can earn.

 

Sample Input

1
5 2 0
2 1 1 1
2 1 1 1
3 2 1 1
4 3 1 1
5 4 1 1

Sample Output

3

Source

 
解题:单调队列优化dp
 考虑买入
$dp[i][j] = max(dp[i-1][j],dp[i - w - 1][k] + (k - j)\times ap$
如何选择k,发现要维护$dp[i-w-1][k] - (m - k)\times ap$
 
可以根据$dp[i-w-1][k] - (m-k)\times ap + (m - j)\times ap$ 计算出 $dp[i - w - 1][k] + (k - j)\times ap\,k < j$
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
int dp[maxn][maxn],q[maxn],p[maxn],hd,tl; int main(){
int kase,ap,bp,as,bs,n,m,w;
scanf("%d",&kase);
while(kase--){
scanf("%d%d%d",&n,&m,&w);
for(int i = ; i <= w + ; ++i){
scanf("%d%d%d%d",&ap,&bp,&as,&bs);
for(int j = ; j <= m; ++j){
dp[i][j] = j <= as?-ap*j:-INF;
if(i > ) dp[i][j] = max(dp[i][j],dp[i-][j]);
}
}
for(int i = w + ; i <= n; ++i){
scanf("%d%d%d%d",&ap,&bp,&as,&bs);
int k = i - w - ;
hd = tl = ;
for(int j = ; j <= m; ++j){
int tmp = dp[k][j] - ap*(m - j);
while(hd < tl && p[tl-] < tmp) --tl;
q[tl] = j;
p[tl++] = tmp;
while(hd < tl && j - q[hd] > as) ++hd;
dp[i][j] = max(dp[i-][j],p[hd] + ap*(m - j));
}
hd = tl = ;
for(int j = m; j >= ; --j){
int tmp = dp[k][j] + bp*j;
while(hd < tl && p[tl-] < tmp) --tl;
q[tl] = j;
p[tl++] = tmp;
while(q[hd] - j > bs) ++hd;
dp[i][j] = max(dp[i][j],p[hd] - bp*j);
}
}
printf("%d\n",dp[n][]);
}
return ;
}

HDU 3401 Trade的更多相关文章

  1. HDU 3401 Trade dp+单调队列优化

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3401 Trade Time Limit: 2000/1000 MS (Java/Others)Mem ...

  2. 【HDU 3401 Trade】 单调队列优化dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 题目大意:现在要你去炒股,给你每天的开盘价值,每股买入价值为ap,卖出价值为bp,每天最多买as ...

  3. HDU 3401 Trade(单调队列优化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:炒股.第i天买入一股的价钱api,卖出一股的价钱bpi,最多买入asi股,最多卖出bsi股 ...

  4. HDU 3401 Trade(斜率优化dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:有一个股市,现在有T天让你炒股,在第i天,买进股票的价格为APi,卖出股票的价格为BPi,同时最多买 ...

  5. 【单调队列优化dp】HDU 3401 Trade

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 [题意] 知道之后n天的股票买卖价格(api,bpi),以及每天股票买卖数量上限(asi,bsi),问他最 ...

  6. hdu 3401 单调队列优化DP

    Trade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  7. hdu 3401 单调队列优化+dp

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 Trade Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  8. HDU——1009FatMouse' Trade(贪心+结构体+排序)

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. 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个 ...

随机推荐

  1. B1277 [HNOI2002]Tinux系统 树形dp

    这个题bzoj上没有图,luogu上样例有问题...其实这个题代码不难,但是思考起来还是有一定难度的,其实这些题的重点都在于思考.我就不写了,洛谷上唯一的题解写的挺好,大家可以看一看. 题干: 在do ...

  2. 腾讯云linux服务器安装lnmp一键包

    这边域名已经实名了. 然后修改DNS服务器 然后备案吧 还是先不备案,直接云解析DNS 哦,想起来了,阿里云自己都可以生成SSL证书.重新弄一次吧,其实腾讯云也可以申请域名型免费版DV

  3. jquery中对于为一组标签赋予点击事件

    可以用each,但是each不能对动态的元素进行事件的绑定, 不过,其实也很简单,只需要获取所有的标签集,然后用动态绑定的方法,比如live进行绑定就可以了. 有时候,其实不难,只是自己想的太过复杂. ...

  4. [Apple开发者帐户帮助]六、配置应用服务(5.3)推送通知(APN):从您的Web服务器发送推送通知

    要使用APN从Web服务器向macOS用户发送推送通知,请注册网站推送标识符并创建网站推送证书. 对于每个网站,请注册一个网站推送标识符,用于验证通知是否来自您的服务器.然后创建一个网站推送证书以签署 ...

  5. [Swift通天遁地]五、高级扩展-(3)日期和时间类型的扩展方法

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. An problem about date 根据年月日计算 星期几

    /W = (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7(1.2月需要看作上一年的13.14月) #include<stdio.h> #include& ...

  7. 查看当前linux版本

    lsb_release -a 如果命令不存在,则yum安装 yum install redhat-lsb

  8. ansible 显示运行时间

    #独家秘诀cd /etc/ansible mkdir callback_plugins cd callback_plugins wget https://raw.githubusercontent.c ...

  9. JavaScript定时器及其他

    By Abyssly Jun 20 2014 Updated:Jun 20 2014 平时工作中不可避免地要嵌套网页,对JavaScript的深入了解还是很有必要滴.而JavaScript中一个容易让 ...

  10. abstract class和interface 抽象类与接口类的区别

    抽象类与类型定义相关: 接口类与行为规范相关: 接口类不是类型. 抽象类:是不完整的类,函数实现未定义:可以继承,不可以实例化. 接口类:接口类不是类:是类间交互的规范:不能继承.不能实例化,只能实现 ...