题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401

  DP方程容易想出来,f[i][j]表示第i天拥有j个股票的最优解,则:

    1、不买不卖,f[i][j]=Max{ f[i][j], f[i-1][j] }。

    2、买进,f[i][j]=Max{ f[i][j], f[pre][k] - (j-k)*ap[i] | j>=k }。

    3、卖出,f[i][j]=Max{ f[i][j], f[pre][k] +(k-j)*bp[i] | k>=j }。

  直接转移复杂度O(n^3),超时。考虑第二种情况,f[pre][k] - (j-k)*ap[i] = (f[pre][k]+k*ap[i])-j*ap[i],用单调队列维护这个值(f[pre][k]+k*ap[i])就可以了。情况3类似。

 //STATUS:C++_AC_375MS_16132KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef long long LL;
typedef unsigned long long ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=1e+,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End
int f[N][N],ap[N],bp[N],as[N],bs[N],q[N*][];
int T,n,m,d;
int main()
{
// freopen("in.txt","r",stdin);
int i,j,k,hig,ans,front,rear,p;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&d);
for(i=;i<=n;i++){
scanf("%d%d%d%d",&ap[i],&bp[i],&as[i],&bs[i]);
}
mem(f,-INF);
for(i=;i<=d+;i++){
for(j=;j<=as[i];j++)
f[i][j]=-j*ap[i];
}
for(i=;i<=d+;i++){
for(j=;j<=m;j++)
f[i][j]=max(f[i][j],f[i-][j]);
}
for(i=d+;i<=n;i++){
p=i-d-;
for(j=;j<=m;j++)f[i][j]=f[i-][j];
//
front=rear=;
q[rear][]=f[p][];q[rear++][]=;
for(j=;j<=m;j++){
while(front<rear && q[front][]<j-as[i])front++;
f[i][j]=max(f[i][j],q[front][]-j*ap[i]);
while(front<rear && q[rear-][]<=f[p][j]+j*ap[i])rear--;
q[rear][]=f[p][j]+j*ap[i];q[rear++][]=j;
}
//
front=rear=;
q[rear][]=f[p][m]+m*bp[i];q[rear++][]=m;
for(j=m-;j>=;j--){
while( front<rear && q[front][]>j+bs[i])front++;
f[i][j]=max(f[i][j],q[front][]-j*bp[i]);
while(front<rear && q[rear-][]<=f[p][j]+j*bp[i])rear--;
q[rear][]=f[p][j]+j*bp[i];q[rear++][]=j;
}
}
ans=;
for(i=;i<=m;i++)ans=max(ans,f[n][i]);
printf("%d\n",ans);
}
return ;
}

HDU-3401 Trade 单调队列优化DP的更多相关文章

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

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

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

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

  3. hdu3401 Trade 单调队列优化dp

    Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. NDK(3)java.lang.UnsatisfiedLinkError: Native method not found解决方法

    调用native方法时报错如下 : “java.lang.UnsatisfiedLinkError: Native method not found....  ”: 原因分析: 链接器只看到了在so中 ...

  2. What is the difference between DAO and DAL?

    What is the difference between DAO and DAL? The Data Access Layer (DAL) is the layer of a system tha ...

  3. in command-line: path> mvn eclipse:clean path> mvn -Dwtpversion=1.5 eclipse:eclipse path> mvn eclipse:eclipse in eclipse: Project / clean...

    原因:tomcat已经启动了 2007-10-9 12:26:16 org.apache.coyote.http11.Http11AprProtocol init严重: Error initializ ...

  4. linux 多处理器概念

    Linux 提出了 Multi-Processing 的概念,它的调度器可以将操作系统的线程均分到各个核(或硬件线程)上去执行,以此达到并行计算的目的,从而也可以极大地提高系统的性能. 实现计数器 1 ...

  5. Java编程思想 (1~10)

    [注:此博客旨在从<Java编程思想>这本书的目录结构上来检验自己的Java基础知识,只为笔记之用] 第一章 对象导论 1.万物皆对象2.程序就是对象的集合3.每个对象都是由其它对象所构成 ...

  6. HDU 2602 (简单的01背包) Bone Collector

    很标准的01背包问题 //#define LOCAL #include <algorithm> #include <cstdio> #include <cstring&g ...

  7. Strom Topology执行分析:worker数,Bolt实例数,executor数,task数

    在创建Storm的Topology时,我们通常使用如下代码:builder.setBolt("cpp", new CppBolt(), 3).setNumTasks(5).none ...

  8. HTML xmlns

    xmlns 属性可以在文档中定义一个或多个可供选择的命名空间.该属性可以放置在文档内任何元素的开始标签中.该属性的值类似于 URL,它定义了一个命名空间,浏览器会将此命名空间用于该属性所在元素内的所有 ...

  9. 使用Spring Session做分布式会话管理

    在Web项目开发中,会话管理是一个很重要的部分,用于存储与用户相关的数据.通常是由符合session规范的容器来负责存储管理,也就是一旦容器关闭,重启会导致会话失效.因此打造一个高可用性的系统,必须将 ...

  10. 一些纯css3写的公司logo

      随着对css3了解得越深入,越来越发现了css3的强大.css3不但能完成一些基本的特效如圆角阴影等,还能借助动画技术实现一些复杂的动画,能替代很多以前js才能完成的工作,css3的作用还不止于此 ...