bzoj1578 [Usaco2009 Feb]Stock Market 股票市场

时间:2024-05-19 10:04:14

传送门: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 + , N = + ;
const int mod = 1e9+; # define RG register
# define ST static int n, m;
int f[N], w[N][N], g[M]; int main() {
cin >> n >> m >> f[];
for (int i=; i<=n; ++i)
for (int j=; j<=m; ++j) cin >> w[i][j]; for (int i=; i<=m; ++i) {
int pre = , cur = ;
for (int k=; k<=f[i-]; ++k)
g[k] = ; for (int j=; j<=n; ++j)
for (int k=; k<=f[i-]; ++k)
if(k >= w[j][i-]) g[k] = max(g[k], g[k-w[j][i-]] + w[j][i]); for (int k=; k<=f[i-]; ++k)
if(g[k] + (f[i-]-k) > f[i]) f[i] = g[k] + (f[i-]-k);
}
cout << f[m]; return ;
}