#include<bits/stdc++.h> #define N 1007 usingnamespacestd; typedeflonglong ll;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int f[1007][1007]; int v[1007], w[1007];
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) { v[i] = rd(); w[i] = rd(); } int ans = 0; memset(f, 0, sizeof(f)); for (int i = 1; i <= n; ++i) { for (int j = 0; j <= m; ++j) { if (j < v[i]) f[i][j] = f[i - 1] [j]; else f[i][j] = max(f[i - 1][j], f[i - 1][j - v[i]] + w[i]); ans = max(ans, f[i][j]); } } printf("%d\n", ans); return0; }
#include<bits/stdc++.h> #define N 1007 usingnamespacestd; typedeflonglong ll;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int f[1007][1007]; int v[1007], w[1007];
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) { v[i] = rd(); w[i] = rd(); } memset(f, 0, sizeof(f)); for (int i = 1; i <= n; ++i) { for (int j = 0; j <= m; ++j) { if (j < v[i]) f[i][j] = f[i - 1] [j]; else f[i][j] = max(f[i - 1][j], f[i - 1][j - v[i]] + w[i]); } } printf("%d\n", f[n][m]); return0; }
压缩空间
滚动数组
因为我们在计算结果时只关心,该层上一层的答案是多少,故我们可用一个临时的一维数组保存下一层的答案。在计算完一层的答案后,将临时数组的值再赋值给数组 f 。
#include<bits/stdc++.h> #define N 1007 usingnamespacestd; typedeflonglong ll;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int f[1007], g[1007], v[1007], w[1007];
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) { v[i] = rd(); w[i] = rd(); } memset(f, 0, sizeof(f)); memset(g, 0, sizeof(g)); for (int i = 1; i <= n; ++i) { for (int j = 0; j <= m; ++j) { if (j < v[i]) g[j] = f[j]; else g[j] = max(f[j], f[j - v[i]] + w[i]); } memcpy(f, g, sizeof(g)); } printf("%d\n", f[m]); return0; }
#include<bits/stdc++.h> #define N 1007 usingnamespacestd; typedeflonglong ll;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int f[1007], v[1007], w[1007];
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) { v[i] = rd(); w[i] = rd(); } memset(f, 0, sizeof(f)); for (int i = 1; i <= n; ++i) { for (int j = m; j >= 0; --j) { if (j < v[i]) f[j] = f[j]; else f[j] = max(f[j], f[j - v[i]] + w[i]); } } printf("%d\n", f[m]); return0; }
#include<bits/stdc++.h> #define N 1007 usingnamespacestd; typedeflonglong ll;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int f[1007][1007], v[1007], w[1007];
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) { v[i] = rd(); w[i] = rd(); } memset(f, 0, sizeof(f)); for (int i = 1; i <= n; ++i) { for (int j = 0; j <= m; ++j) { if (j < v[i]) f[i][j] = f[i - 1][j]; else f[i][j] = max(f[i - 1][j], f[i][j - v[i]] + w[i]); } } printf("%d\n", f[n][m]); return0; }
#include<bits/stdc++.h> #define N 1007 usingnamespacestd; typedeflonglong ll;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int f[1007], v[1007], w[1007];
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) { v[i] = rd(); w[i] = rd(); } memset(f, 0, sizeof(f)); for (int i = 1; i <= n; ++i) { for (int j = 0; j <= m; ++j) { if (j < v[i]) f[j] = f[j]; else f[j] = max(f[j], f[j - v[i]] + w[i]); } } printf("%d\n", f[m]); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int f[1007], v[1007], w[1007], l[1007];
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) { v[i] = rd(); w[i] = rd(); l[i] = rd(); } memset(f, 0, sizeof(f)); for (int i = 1; i <= n; ++i) { for (int k = 1; k <= l[i]; ++k) for (int j = m; j >= 0; --j) { if (j < v[i]) f[j] = f[j]; else f[j] = max(f[j], f[j - v[i]] + w[i]); } } printf("%d\n", f[m]); return0; }
#include<bits/stdc++.h> #define N 1007 usingnamespacestd; typedeflonglong ll;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int f[2007], v[2007], w[2007], l[2007];
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) { v[i] = rd(); w[i] = rd(); l[i] = rd(); } for (int i = 1; i <= n; ++i) { int res = l[i]; for (int k = 1; k <= res; res -= k, k <<= 2) { int lim = v[i] * k, val = w[i] * k; for (int j = m; j >= lim; --j) f[j] = max(f[j], f[j - lim] + val); } int lim = v[i] * res, val = w[i] * res; for (int j = m; j >= lim; --j) f[j] = max(f[j], f[j - lim] + val); } printf("%d\n", f[m]); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int c[100007][2], a[100007], b[100007];
intmain(){ int n = rd(); for (int i = 1; i <= n; ++i) { a[i] = rd(); b[i] = rd(); } int k = 0, l = 1; for (int i = 1; i <= n; ++i) { for (; k >= l && b[i] >= c[k][0]; --k); c[++k][0] = b[i]; c[k][1] = a[i]; printf("%d\n", c[l][0]); for(; k >= l && c[l][1] == i; ++l); } return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int f[1007][1007], a[1007], v[1007], w[1007]; vector<int> c[1007];
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) { a[i] = rd(); v[i] = rd(); w[i] = rd(); c[a[i]].push_back(i); } for (int i = 1; i <= 1000; ++i) { for (int j = 0; j <= m; ++j) { f[i][j] = f[i - 1][j]; for (auto k : c[i]) if (v[k] <= j) f[i][j] = max(f[i][j], f[i - 1][j - v[k]] + w[k]); } } printf("%d\n", f[1000][m]); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int f[1007], a[1007], v[1007], w[1007]; vector<int> c[1007];
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) { a[i] = rd(); v[i] = rd(); w[i] = rd(); c[a[i]].push_back(i); } for (int i = 1; i <= 1000; ++i) for (int j = m; j; --j) for (auto k : c[i]) if (v[k] <= j) f[j] = max(f[j], f[j - v[k]] + w[k]); printf("%d\n", f[m]); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int f[107][107][107], t[107], v[107], w[107];
intmain(){ int n = rd(), m = rd(), k = rd(); for (int i = 1; i <= n; ++i) { v[i] = rd(); w[i] = rd(); t[i] = rd(); } for (int i = 1; i <= n; ++i) { for (int j = 0; j <= m; ++j) { for (int x = 0; x <= k; ++x) { f[i][j][x] = f[i - 1][j][x]; if (v[i] <= j && t[i] <= x) f[i][j][x] = max(f[i][j][x], f[i - 1][j - v[i]][x - t[i]] + w[i]); } } } printf("%d\n", f[n][m][k]); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int f[107][107], t[107], v[107], w[107];
intmain(){ int n = rd(), m = rd(), k = rd(); for (int i = 1; i <= n; ++i) { v[i] = rd(); w[i] = rd(); t[i] = rd(); } for (int i = 1; i <= n; ++i) for (int j = m; j >= v[i]; --j) for (int x = k; x >= t[i]; --x) f[j][x] = max(f[j][x], f[j - v[i]][x - t[i]] + w[i]); printf("%d\n", f[m][k]); return0; }
Post title:背包
Post author:Eva.Q
Create time:2022-01-20 09:25:17
Post link:https://qyy/2022/01/20/Algorithm/DP1/
Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.