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