bzoj1578 [Usaco2009 Feb]Stock Market 股票市场
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1578
【题解】
由于连续买相当于每天买,第二天卖,然后再买。所以每天最后钱尽量多一定是最优的。
所以对于m天,每天做一次O(n*70w)的完全背包dp即可。
# include <stdio.h> # include <string.h> # include <iostream> # include <algorithm> // # include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; const int M = 7e5 + 10, N = 100 + 10; const int mod = 1e9+7; # define RG register # define ST static int n, m; int f[N], w[N][N], g[M]; int main() { cin >> n >> m >> f[1]; for (int i=1; i<=n; ++i) for (int j=1; j<=m; ++j) cin >> w[i][j]; for (int i=2; i<=m; ++i) { int pre = 0, cur = 1; for (int k=0; k<=f[i-1]; ++k) g[k] = 0; for (int j=1; j<=n; ++j) for (int k=0; k<=f[i-1]; ++k) if(k >= w[j][i-1]) g[k] = max(g[k], g[k-w[j][i-1]] + w[j][i]); for (int k=0; k<=f[i-1]; ++k) if(g[k] + (f[i-1]-k) > f[i]) f[i] = g[k] + (f[i-1]-k); } cout << f[m]; return 0; }