BZOJ1578: [Usaco2009 Feb]Stock Market 股票市场
S<=50只股票D<=10天的价格给出,求第一天开始用n<=200000元最后能得到的最大钱数,保证答案<=500000。
做D次完全背包即可,每次做完把dp数组清空。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdlib> 5 //#include<iostream> 6 using namespace std; 7 8 int S,D,n; 9 #define maxn 500011 10 int f[maxn],a[55][15],cur=0; 11 int main() 12 { 13 scanf("%d%d%d",&S,&D,&n); 14 for (int i=1;i<=S;i++) 15 for (int j=1;j<=D;j++) 16 scanf("%d",&a[i][j]); 17 for (int t=1;t<D;t++) 18 { 19 memset(f,0,sizeof(f)); 20 int w,v; 21 for (int i=1;i<=S;i++) 22 { 23 cur^=1; 24 w=a[i][t]; 25 v=a[i][t+1]-a[i][t]; 26 for (int j=0;j<w;j++) 27 { 28 int ans=0; 29 for (int k=0,now=k*w+j;now<=n;k++,now+=w) 30 { 31 ans=max(ans,f[now]-k*v); 32 f[now]=ans+k*v; 33 } 34 } 35 } 36 n+=f[n]; 37 } 38 printf("%d\n",n); 39 return 0; 40 }