[zoj3623]背包模型
给定n种物品,每种物品需要ti时间生产出来,生产出来以后,每单位时间可以创造wi个价值。如果需要创造至少W个价值,求最少时间。
思路:dp[j]表示用时间j所能创造的最大价值,则有转移方程:dp[j + t[i]] = max(dp[j + t[i], dp[j] + t * w[i]])。另外是否需要按一定顺序排序呢??以下是ac代码。
1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #include <map> 9 #include <queue> 10 #include <deque> 11 #include <cmath> 12 #include <vector> 13 #include <ctime> 14 #include <cctype> 15 #include <set> 16 17 using namespace std; 18 19 #define mem0(a) memset(a, 0, sizeof(a)) 20 #define lson l, m, rt << 1 21 #define rson m + 1, r, rt << 1 | 1 22 #define define_m int m = (l + r) >> 1 23 #define rep(a, b) for (int a = 0; a < (b); a++) 24 #define rep1(a, b) for (int a = 1; a <= (b); a++) 25 #define all(a) (a).begin(), (a).end() 26 #define lowbit(x) ((x) & (-(x))) 27 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {} 28 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {} 29 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} 30 #define pc(a) putchar(a) 31 #define ps(a) printf("%s", a) 32 #define pd(a) printf("%d", a) 33 #define sd(a) scanf("%d", &a) 34 35 typedef double db; 36 typedef long long LL; 37 typedef pair<int, int> pii; 38 typedef multiset<int> msi; 39 typedef set<int> si; 40 typedef vector<int> vi; 41 typedef map<int, int> mii; 42 43 const int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1}; 44 const int dy[8] = {1, 0, -1, 0, -1, 1, 1, -1}; 45 const int maxn = 1e5 + 7; 46 const int maxm = 1e5 + 7; 47 const int maxv = 1e7 + 7; 48 const int max_val = 1e6 + 7; 49 const int MD = 1e9 +7; 50 const int INF = 1e9 + 7; 51 const double PI = acos(-1.0); 52 const double eps = 1e-10; 53 54 template<class T> T gcd(T a, T b) { return b == 0? a : gcd(b, a % b); } 55 56 int f[700], a[40], b[40]; 57 58 int main() { 59 //freopen("in.txt", "r", stdin); 60 int n, l; 61 while (cin >> n >> l) { 62 rep(i, n) { 63 sd(a[i]); 64 sd(b[i]); 65 } 66 int maxt = 330; 67 mem0(f); 68 rep(i, n) { 69 rep(j, maxt) { 70 f[j + a[i]] = max(f[j + a[i]], f[j] + j * b[i]); 71 } 72 } 73 int ans; 74 rep(i, maxt * 2) { 75 if (f[i] >= l) { 76 ans = i; 77 break; 78 } 79 } 80 cout << ans << endl; 81 } 82 return 0; 83 }