【BZOJ1578】【USACO2009Feb】股票市场 背包DP

题目大意

  告诉你(n)只股票在这(m)天内的价格,给你(s)元的初始资金,问你(m)天后你最多拥有多少钱。

  (nleq 50,mleq 10,sleq 200000,)答案(leq 500000)

题解

  首先我们可以认为第(i)天买的所有股票都会在第(i+1)天卖出(如果不卖出就可以看成卖出再买入)。

  那么我们可以把第(i)天的价格看成体积,把第(i+1)天的价格(-)(i)天的价格看成价值,做(m-1)次完全背包即可。

  时间复杂度:(O(nms))

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<cmath>
#include<functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void sort(int &a,int &b)
{
	if(a>b)
		swap(a,b);
}
void open(const char *s)
{
#ifndef ONLINE_JUDGE
	char str[100];
	sprintf(str,"%s.in",s);
	freopen(str,"r",stdin);
	sprintf(str,"%s.out",s);
	freopen(str,"w",stdout);
#endif
}
int f[500010];
int a[110][110];
int main()
{
	int n,m,u;
	scanf("%d%d%d",&n,&m,&u);
	int i,j,k;
	for(j=1;j<=n;j++)
		for(i=1;i<=m;i++)
			scanf("%d",&a[i][j]);
	for(i=2;i<=m;i++)
	{
		memset(f,0,sizeof f);
		for(j=1;j<=n;j++)
		{
			int v=a[i][j]-a[i-1][j],w=a[i-1][j];
			if(v<0)
				continue;
			for(k=w;k<=u;k++)
				f[k]=max(f[k],f[k-w]+v);
		}
		u+=*max_element(f+1,f+u+1);
	}
	printf("%d
",u);
	return 0;
}
原文地址:https://www.cnblogs.com/ywwyww/p/8513220.html