【HDU】3401Trade贸易

【HDU】3401Trade贸易

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)

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

翻译

贸易

时间限制:2000/1000 MS(Java /其他)
内存限制:32768/32768 K(Java /其他)

问题描述

最近,lxhgww沉迷于股票,经过几天的研究,他发现了一些规律的模式。
他预测未来T天的股票市场。在第i天,您可以以APi的价格购买一只股票或卖出一只股票以获得BPi。
还有其他一些限制,即在第i天最多可以购买ASi股票,最多可以出售BSi股票。
两个交易日的间隔应大于W天。也就是说,假设您在第i天进行了交易(任何买卖股票都被视为一项交易),那么下一个交易日必须在第(i + W + 1)天或更晚的时间进行。
而且,任何时候最多只能拥有MaxP股票。

在第一天之前,lxhgww已经拥有无限的资金,但是没有股票,当然,他希望从股市中赚到尽可能多的钱。所以问题来了,他最多只能赚多少钱?

输入

第一行是整数t,即案例编号。
每种情况的第一行是三个整数T,MaxP,W。
(0 <= W <T <= 2000,1 <= MaxP <= 2000)。
接下来的T线各具有四个整数APi,BPi,ASi,BSi(1 <= BPi <= APi <= 1000,1 <= ASi,BSi <= MaxP)。

输出

lxhgww可以赚到最多的钱。

样本输入

1
5 2 0
2 1 1 1
2 1 1 1
3 2 1 1
4 3 1 1
5 4 1 1

样本输出

3

思路

用DP做
状态转移方程:

扫描二维码关注公众号,回复: 12800975 查看本文章
f[i][j]=max(f[i-1][j],max(f[r][k]-AP[i]*(j-k))(0<r<i-w,k<j),max(f[r][k]+BP[i]*(k-j))(0<r<i-w,k>j))


max(dp[r][k]-AP[i](j-k))
max(dp[r][k]+BP[i]
(k-j))
用单调队列优化

代码

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int f[2010][2010],s[2010],x[2010],ap[2010],bp[2010],as[2010],bs[2010];
int main()
{
    
    
	int T,n,maxp,w,i,j,l,r,ans;
	for(scanf("%d",&T);T;T--)
	{
    
    
		scanf("%d%d%d",&n,&maxp,&w);
		memset(f,0xcf,sizeof(f));//置最小值 
		for(ans=-0x7f7f7f7f,i=1;i<=n;i++) 
			scanf("%d%d%d%d",&ap[i],&bp[i],&as[i],&bs[i]);
		for(i=1;i<=w+1;i++)
			for(j=0;j<=as[i];j++) 
				f[i][j]=-ap[i]*j;//初始化
		for(i=2;i<=n;i++)
		{
    
    
			for(j=0;j<=maxp;j++)//不买卖的价值 
				f[i][j]=max(f[i][j],f[i-1][j]);
			if(i>w+1)//能买卖时
			{
    
    
				for(l=r=1,j=0;j<=maxp;j++)//枚举买件数 
				{
    
    
					for(;l<r&&s[r-1]<f[i-w-1][j]+ap[i]*j;r--);//删除小于当前价值的数 
					s[r]=f[i-w-1][j]+ap[i]*j,x[r]=j;//加入队列
					for(r++;l<r&&x[l]+as[i]<j;l++);//删除超限的数
					f[i][j]=max(f[i][j],s[l]-ap[i]*j);
				}
				for(l=r=1,j=maxp;j>=0;j--)//枚举卖件数
				{
    
    
					for(;l<r&&s[r-1]<f[i-w-1][j]+bp[i]*j;r--);//删除小于当前价值的数
					s[r]=f[i-w-1][j]+bp[i]*j,x[r]=j;//加入队列
					for(r++;l<r&&x[l]-bs[i]>j;l++);//删除超限的数
					f[i][j]=max(f[i][j],s[l]-bp[i]*j);
				}
			}
		}
		for(i=0;i<=maxp;i++)//找最大值 
			ans=max(ans,f[n][i]);
		printf("%d\n",ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46975572/article/details/114671792