Trade

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2918    Accepted Submission(s): 930

Problem Description
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
 
Author
lxhgww
 
Source
 
Recommend
lcy
这里错了好多次了,首先,我们可以得出dp[i][j]=fmax(dp[i][k]+ap[ii]*(k-j),dp[i][k]+bp[ii]*(k-j)) dp[i][j]表求第i天有j只,这样,复杂度太高了,我们可以发现,我们就可以通过dp[i][j]的先一天得到最大值,这样我们就可以减成n^3了,我们可以把dp[i][k]+bp[i]*k 和dp[i][k]+ap[i][k],用一个单调队列存起来,这样可以优化成了n^2,也就可以过了,但是要注意,这里,一定要把小于m的开始单独初始化,这里易错!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 2005
#define inf -1000000000
int ap[MAXN],bp[MAXN],as[MAXN],bs[MAXN],dp[MAXN][MAXN],queuea[MAXN],queueb[MAXN],maxp;
int fmax(int a,int b)
{
if(a>b)
return a;
return b;
}
int ffa(int i,int k,int ii)
{
return dp[i][k]+k*bp[ii];
}
int ffb(int i,int k,int ii)
{
return dp[i][k]-(maxp-k)*ap[ii];
}
int main()
{
int tcase,i,ii,j,k,n,m,s,e,l,r;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d%d%d",&n,&maxp,&m);
for(i=1;i<=n;i++)
{
scanf("%d%d%d%d",&ap[i],&bp[i],&as[i],&bs[i]);
}
for(i=1;i<=m+1;i++)
for(j=0;j<=maxp;j++)
{
dp[i][j]=(j<=as[i])?-ap[i]*j:inf;
if(i>1)dp[i][j]=fmax(dp[i][j],dp[i-1][j]);
}
for(ii=m+2;ii<=n;ii++)
{
i=ii-m-1; l=0;r=-1;
for(j=0;j<=maxp;j++)
{
while(l<=r&&ffb(i,j,ii)>ffb(i,queueb[r],ii))
{
r--;
}
queueb[++r]=j;
while(((j-queueb[l]>as[ii])))
{
l++;
}
k=queueb[l];
dp[ii][j]=fmax(dp[ii-1][j],dp[i][k]+ap[ii]*(k-j));
}
s=0;e=-1; ; for(j=maxp;j>=0;j--)
{ while(s<=e&&ffa(i,j,ii)>ffa(i,queuea[e],ii))
{
e--;
}
queuea[++e]=j;
while((queuea[s]-j>bs[ii]))
{
s++;
}
k=queuea[s];
dp[ii][j]=fmax(dp[ii][j],dp[i][k]+bp[ii]*(k-j)); } }
int maxx=dp[n][0];
printf("%d\n",dp[n][0]);
} return 0;
}

hdu3401 Trade 单调队列优化dp的更多相关文章

  1. HDU-3401 Trade 单调队列优化DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 DP方程容易想出来,f[i][j]表示第i天拥有j个股票的最优解,则: 1.不买不卖,f[i][ ...

  2. hdu3401:单调队列优化dp

    第一个单调队列优化dp 写了半天,最后初始化搞错了还一直wa.. 题目大意: 炒股,总共 t 天,每天可以买入na[i]股,卖出nb[i]股,价钱分别为pa[i]和pb[i],最大同时拥有p股 且一次 ...

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

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

  4. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

  5. 1855: [Scoi2010]股票交易[单调队列优化DP]

    1855: [Scoi2010]股票交易 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1083  Solved: 519[Submit][Status] ...

  6. 单调队列优化DP——习题收集

    前言 感觉可以用单调队列优化dp的模型还是挺活的,开个随笔记录一些遇到的比较有代表性的模型,断续更新.主要做一个收集整理总结工作. 记录 0x01 POJ - 1821 Fence,比较适合入门的题, ...

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

  8. Parade(单调队列优化dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others)    ...

  9. BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP

    BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP Description 有一排n棵树,第i棵树的高度是Di. MHY要从第一棵树到第n棵树去找他的妹子玩. 如果MHY在 ...

随机推荐

  1. Linux内核中内存cache的实现【转】

    Linux内核中内存cache的实现 转自:http://blog.chinaunix.net/uid-127037-id-2919545.html   本文档的Copyleft归yfydz所有,使用 ...

  2. PhysX SDK src

    PhysX SDK src Physx3.3 source code http://download.csdn.net/download/qq122252656/9427387 Nvidia CUDA ...

  3. UNIX shell 学习笔记 一 : 几个shell的规则语法对比

    1. 查看系统有哪些可用的shell cat /etc/shell 2. 每种shell都有一个特殊内置变量来存上一条命令的退出状态,例: C/TC shell $status % cp fx fy ...

  4. aspxgridview export导出数据,把true显示成‘是’

    项目原因,数据库中的数据是‘true’还有‘false’,但是在页面上要显示为‘是否’,导出来的时候也是要显示成‘是否’ 要在web页面当中显示成‘是否’,只要在gridview的CustomColu ...

  5. 让我们来一起学习OC吧

    在本分类中的接下来的将翻译http://rypress.com/tutorials/objective-c/index 通过每一章节的翻译,使得自己的OC基础扎实并分享给大家.

  6. udp调用connect有什么作用(转)

    原文链接如下: http://blog.csdn.net/wannew/article/details/18218619 整理一下.1:UDP中可以使用connect系统调用 2:UDP中connec ...

  7. ZOJ-3319

    Islands Time Limit: 1 Second      Memory Limit: 32768 KB There are N islands and some directed paths ...

  8. J2EE MySQL Date数据保持一致解决方案

    1.设置MySQL时区,明确指定 MySQL 数据库的时区,不使用引发误解的 CST show variables like '%time_zone%';set global time_zone = ...

  9. AC日记——餐巾计划问题 洛谷 P1084

    餐巾计划问题 思路: 氧气优化水过: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 4005 #define ...

  10. CentOS7配置sentinel高可用redis

    redis哨兵:用于管理和实现多个redis组实现高可用,sentinel哨兵只监控主节点,因为主节点上有所有的从节点信息,当master节点发生故障,sentinel之间会进行投票选举一个slave ...