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. 神经网络结构设计指导原则——输入层:神经元个数=feature维度 输出层:神经元个数=分类类别数,默认只用一个隐层 如果用多个隐层,则每个隐层的神经元数目都一样

    神经网络结构设计指导原则 原文   http://blog.csdn.net/ybdesire/article/details/52821185   下面这个神经网络结构设计指导原则是Andrew N ...

  2. 【BZOJ 2457】 双端队列

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2457 [算法] 贪心 [代码] #include<bits/stdc++.h& ...

  3. [linux环境配置]个人用持续更新ing~

    alias ll='ls -la' export PATH=$PATH:~/Desktop/myscript alias gpush='git push origin HEAD:refs/for/ma ...

  4. 如何看待B站疑似源码泄漏的问题?

    今天突然看到关于B站源码泄漏事.网曝B站整个网站后台工程源码遭泄露,开源项目平台Github上疑似出现了Bilibili网站后台工程,内含部分用户名密码.目前官方还没对此事作出任何回应,所以还无法确定 ...

  5. Java中static方法

    今天学习到了并且应用到了java中的静态方法,并且了解到它的好处与缺点. ● 生命周期(Lifecycle): 静态方法(Static Method)与静态成员变量一样,属于类本身,在类装载的时候被装 ...

  6. 【BZOJ3294/洛谷3158】[CQOI2011]放棋子(组合数+DP)

    题目: 洛谷3158 分析: 某OIer兔崽子的此题代码中的三个函数名:dfs.ddfs.dddfs(充满毒瘤的气息 显然,行与行之间.列与列之间是互相独立的.考虑背包,用\(f[k][i][j]\) ...

  7. android sqlite中判断某个表是否存在

    <span style="font-size:18px;">sqlite 中判断某个表是否存在的方法,贴出来供大家参考 /** * 判断某张表是否存在 * @param ...

  8. [ BZOJ 2134 ] 单选错位

    \(\\\) \(Description\) 一共\(N​\)道题目,第\(i​\)道题有\(A_i​\)个选项,现在有一个人做完了所有题目,但将每一道题的答案都写到了下一道题的位置\((​\)第\( ...

  9. Java 开源博客 Solo 1.3.0 发布 - Docker 支持

    Solo 1.3.0 正式发布了,感谢一直以来关注 B3log 开源的朋友! 可以通过一个命令启动(不需要安装数据库.部署容器),也可以通过 war 方式部署容器,连接 MySQL.这应该是史上最容易 ...

  10. Android进入一个新页面,EditText失去焦点并禁止弹出键盘

    android在进入一个新页面后,edittext会自动获取焦点并弹出软键盘,这样并不符合用户操作习惯. 在其父控件下,添加如下的属性,就可以完美解决,使其进入页面后不主动获取焦点,并且不弹出软键盘: ...