题目链接: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 ;
}

最新文章

  1. hibernate中数据库方言
  2. IE11 iframe alternative
  3. Java多线程系列--“JUC锁”08之 共享锁和ReentrantReadWriteLock
  4. iOS加密之MD5加密
  5. 详细整合教程(Spring+SpringMVC+MyBatis)
  6. acpi参考网站
  7. ddl dml dcl
  8. Neo4j图数据库管理系统开发笔记之三:构建安全的RMI Service(Server)
  9. 【兄弟连】2016高洛峰新版PHP培训视频教程
  10. 【python常用模块】os.path
  11. Lucene全文检索的【增、删、改、查】 实例
  12. volatile(C# 参考)
  13. 【Linux】使用ZStack私有云创建本地Linux服务器
  14. 在lua环境中使用protobuf
  15. 用pthon来写个跳板机
  16. dede头 名字 和关键字的调用
  17. jenkins API
  18. react实例:理解dva构建项目的原理
  19. VC调用QT的UIDLL
  20. Unity3D手游开发日记(2) - 技能系统架构设计

热门文章

  1. visual studio 2015常用快捷键
  2. iPad中控制器view初始的width和height
  3. IOS中控制器的重要方法使用
  4. ecshop 分类树全部显示
  5. java中正则表达式的应用
  6. 将UE添加到右键菜单
  7. 收缩Mysql的ibdata1文件大小方法
  8. js浮点数运算需要注意的问题
  9. AutoCompleteTextView不能使用的问题
  10. 4. 2D绘制与控件绘制