http://acm.hdu.edu.cn/showproblem.php?pid=3401

Trade

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

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
  感觉初始化有点坑,,,,
由于间隔w天才能进行第二次购买,对于第i天想进行交易的股票来说之前的w天如果可以转移说明之前没进行过交易,所以这几天的数值都一样,所以不必重复的更新,
只从前i-w-1这一天转移就好了。
so  f[i][j]=MAX{f[i][j-1], f[i-w-1][k]-(j-k)*ap[i] k<=j&&j-k<=as[i], f[i-w-1][k]+(k-j)*bp[i] | k>=j&&k-j<=bs[i] };
显然可以用单调队列加速,但是注意要提前处理前w天所有购买后的花费, 因为对于i-w-1小于零的天我们直接忽略,导致并没有计算他们,所以切记初始化!
不必考虑前w+1天卖,因为如果想卖说明前面买了,但是两天都处于前w+1天中间隔肯定小于w天,因此证明得前w+1天里不可能发生卖的操作!
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define inf 120
#define LL long long
inline int read(int f = )
{
char c = getchar();while (!isdigit(c)) { if (c == '-')f = -; c = getchar(); }
int r = ; while (isdigit(c)) { r = r * + c - '';c = getchar(); } return r*f;
}
int f[][];
int ap[], bp[], as[], bs[];
struct node2 { int w, k; };
deque<node2>q;
int main()
{
int i, j, k, cas, T, MAX_P, W;
cin >> cas;
while (cas--) {
T = read();
MAX_P = read();
W = read();
for (i = ;i <= T;++i)
{
ap[i] = read();
bp[i] = read();
as[i] = read();
bs[i] = read();
}
memset(f, -inf, sizeof(f));
for (int i = ; i <= W + ; i++) {//第一天到W+1天只都是只能买的
for (int j = ; j <= min(MAX_P, as[i]); j++) {
f[i][j] = -ap[i] * j;
}
}
f[][] = ;
for (i = ;i <= T;++i)
{
int u = i - W - ;
for (j = ;j <= MAX_P;++j)
{
f[i][j] = max(f[i][j],f[i - ][j]);
if (u < )continue;
while (!q.empty() && q.back().w < f[u][j] + j*ap[i])q.pop_back();
q.push_back(node2{f[u][j]+j*ap[i],j});
while (!q.empty() && j - q.front().k > as[i])q.pop_front();
if (!q.empty()) f[i][j] = max(f[i][j], q.front().w - j*ap[i]);
}
if (u < )continue;
q.clear();
for (j = MAX_P;j >= ;--j)
{
while (!q.empty() && q.back().w < f[u][j] + j*bp[i])q.pop_back();
q.push_back(node2{f[u][j]+j*bp[i],j});
while (!q.empty() && q.front().k -j> bs[i])q.pop_front();
if (!q.empty()) f[i][j] = max(f[i][j], q.front().w - j*bp[i]);
}
q.clear();
}
int ans = ;
for (i = ;i <= MAX_P;++i)
ans = max(ans,f[T][i]);
printf("%d\n", ans);
}
return ;
}

最新文章

  1. C# 索引器,实现IEnumerable接口的GetEnumerator()方法
  2. ASP.NET MVC多表单提交
  3. Javascript高级程序设计——面向对象之实现继承
  4. [eclipse] Server at localhost was unable to start within 45 seconds.
  5. transform scale
  6. eclipse下tomcat添加部署Module,Web名称与项目名称不一致的解决方法
  7. Hibernate SQL方言 (hibernate.dialect)
  8. jquery ListBox 左右移动
  9. 高仿qq聊天界面
  10. HDU 4983 Goffi and GCD
  11. hdu 2871 Memory Control(伸展树splay tree)
  12. JSTL 核心标签库 使用(转)
  13. TCP/IP协议、UDP协议、 Http协议
  14. linux下如何批量杀JAVA进程或某个进程方法
  15. 18. 4Sum(双指针)
  16. 有关String的一些方法的使用
  17. Winform 实现无边框窗体移动功能
  18. 在jQuery定义自己的扩展方法函数
  19. vue-router的link样式设置问题
  20. VC孙鑫老师第八课:你能捉到我吗?

热门文章

  1. 使用远程管理工具Xshell
  2. Linux中的系统默认日志
  3. SQLServer: 用 ApexSQLLog 恢复 SQL Server 数据
  4. (转)js获取内网ip地址,操作系统,浏览器版本等信息
  5. 前端基础之css样式(选择器)
  6. (转) GIS 中地理坐标和屏幕坐标的标准转换方法
  7. VC6.0 开发 64 位程序
  8. CentOS7在VMWare12中安装后不能上网解决办法
  9. vi的搜索和替换
  10. MySQL-5.7权限详解