题目:6. 多重背包问题 III
分析:
单调队列优化多重背包,时间复杂度O(N * V)
(也可用二进制优化做,不过复杂度多一个
l
o
g
log
log)
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 3e4;
const int inf = 0x3f3f3f3f;
int n, m;
int dp[maxn], last[maxn], q[maxn];
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
int w, v, num; cin >> w >> v >> num;
memcpy(last, dp, sizeof(dp));
for(int j = 0; j < w; j++)
{
int l = 0, r = -1;
for(int k = j; k <= m; k += w)
{
while(l <= r && q[l] < k - num * w) l++;
while(l <= r && last[k] > last[q[l]] + (k - q[l]) / w * v) r--;
q[++r] = k;
dp[k] = last[q[l]] + (k - q[l]) / w * v;
}
}
}
cout << dp[m] << endl;
}
题目:1020. 潜水员
分析:
dp[i][j]表示两个费用至少是i和至少是j的情况下,重量的最小值。
注意,从大到小枚举费用的时候,最小到0。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 2e6;
const int inf = 0x3f3f3f3f;
const int N = 22, M = 80;
int v1, v2;
int dp[200][200];
int main()
{
cin >> v1 >> v2;
int k; cin >> k;
mem(dp, inf);
dp[0][0] = 0;
for(int i = 1; i <= k; i++)
{
int w1, w2, v; cin >> w1 >> w2 >> v;
for(int j = N; j >= 0; j--)
for(int k = M; k >= 0; k--)
dp[j][k] = min(dp[j][k], dp[max(j - w1, 0)][max(0, k - w2)] + v);
}
cout << dp[v1][v2] << endl;
}
题目:12. 背包问题求具体方案
分析:
01背包求最小字典序方案。
先倒着dp,然后正着遍历每个物品,开始的体积为背包容量,如果当前物品可以选也可以不选,那么就选。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 2e6;
const int inf = 0x3f3f3f3f;
int n, v;
int dp[1010][1010];
int c[1010], val[1010];
int main()
{
cin >> n >> v;
for(int i = 1; i <= n; i++) cin >> c[i] >> val[i];
for(int i = n; i >= 1; i--)
{
for(int j = 0; j <= v; j++)
{
dp[i][j] = dp[i + 1][j];
if(j >= c[i]) dp[i][j] = max(dp[i][j], dp[i + 1][j - c[i]] + val[i]);
}
}
vector<int> res;
int now = v;
for(int i = 1; i <= n; i++)
{
if(now >= c[i] && dp[i][now] == dp[i + 1][now - c[i]] + val[i])
res.push_back(i), now -= c[i];
}
for(int i = 0; i < res.size(); i++)
printf("%d%c", res[i], i == res.size() - 1 ? '\n' : ' ');
}
题目:7. 混合背包问题
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 2e6;
const int inf = 0x3f3f3f3f;
int dp[maxn];
int main()
{
int n, V; cin >> n >> V;
for(int i = 1; i <= n; i++)
{
int w, v, s; cin >> w >> v >> s;
if(!s)
{
for(int j = w; j <= V; j++)
dp[j] = max(dp[j], dp[j - w] + v);
}
else
{
if(s == -1) s = 1;
for(int k = 1; k <= s; k <<= 1)
{
for(int j = V; j >= k * w; j--)
dp[j] = max(dp[j], dp[j - k * w] + k * v);
s -= k;
}
if(s)
for(int j = V; j >= s * w; j--)
dp[j] = max(dp[j], dp[j - s * w] + s * v);
}
}
cout << dp[V] << endl;
}
题目:532. 货币系统
分析:
先对货币从小到打排序,然后跑完全背包的方案数,对于第i个物品,如果dp[w[i]] = 0,则表示该物品存在于最小独立集中。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 3e4;
const int inf = 0x3f3f3f3f;
int dp[maxn];
int a[maxn];
int main()
{
int t; cin >> t;
while(t--)
{
int n; cin >> n;
mem(dp, 0);
dp[0] = 1;
for(int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + 1 + n);
int res = 0;
for(int i = 1; i <= n; i++)
{
if(!dp[a[i]]) res++;
for(int j = a[i]; j <= a[n]; j++)
dp[j] += dp[j - a[i]];
}
cout << res << endl;
}
}
题目:10. 有依赖的背包问题
分析:
dp[i][j]代表以i为根节点的子树在背包容量为j的情况下,所能产生的最大价值。计算时先扣除根节点在背包中的体积占用。最后再补上。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 110;
const int inf = 0x3f3f3f3f;
struct edge
{
int ep, nex;
edge(int ep = 0, int nex = 0) : ep(ep), nex(nex){}
}e[maxn];
int head[maxn], tot;
int dp[maxn][maxn];
int n, v;
void init()
{
mem(head, -1), tot = 0;
}
void addedge(int x, int y)
{
e[tot] = edge(y, head[x]);
head[x] = tot++;
}
int w[maxn], val[maxn];
void dfs(int u)
{
for(int i = head[u]; ~i; i = e[i].nex)
{
int ep = e[i].ep;
dfs(ep);
for(int j = v - w[u]; j >= 0; j--)
for(int k = 0; k <= j; k++)
dp[u][j] = max(dp[u][j], dp[u][j - k] + dp[ep][k]);
}
for(int i = 0; i <= v - w[u]; i++)
dp[u][i + w[u]] = dp[u][i] + val[u];
for(int i = 0; i <= w[u]; i++)
dp[u][0] = 0;
}
int main()
{
cin >> n >> v;
init();
int root = -1;
for(int i = 1; i <= n; i++)
{
int f;
cin >> w[i] >> val[i] >> f;
if(f == -1) root = i;
else addedge(f, i);
}
dfs(root);
cout << dp[root][v] << endl;
}
for(int i = 1; i <)
题目:11. 背包问题求方案数
分析:
这个题是求最大价值所对应的方案数。
我们设dp[i][j]表示考虑前i个物品,恰好占用j的体积的情况下的最大价值,num保存其对应的方案数。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 1010;
const int inf = 0x3f3f3f3f;
int n, v;
int dp[maxn], num[maxn];
signed main()
{
cin >> n >> v;
mem(dp, -inf), dp[0] = 0, num[0] = 1;
for(int i = 1; i <= n; i++)
{
int w, val; cin >> w >> val;
for(int j = v; j >= w; j--)
{
ll cnt = 0;
ll maxx = max(dp[j], dp[j - w] + val);
if(maxx == dp[j]) cnt += num[j];
if(maxx == dp[j - w] + val) cnt += num[j - w];
dp[j] = maxx, num[j] = cnt % mod;
}
}
int maxx = -1;
for(int i = 1; i <= v; i++) maxx = max(maxx, dp[i]);
int res = 0;
for(int i = 1; i <= v; i++)
if(dp[i] == maxx)
res += num[i], res %= mod;
cout << res << endl;
}
题目:734. 能量石
分析:
设有两个能量石,先吃能量石1,获得的能量是e1 + e2 - l2 * s1;先吃能量石2,获得的能量是e2 + e1 - l1 * s2;要使e1 + e2 - l2 * s1 > e2 + e1 - l1 * s2,只需要l2 * s1 < l1*s2即可,即按照s / l自小到大的顺序吃能量石即可。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 100000;
const int inf = 0x3f3f3f3f;
struct node
{
int e, s, l;
bool operator< (const node& b) const
{
return b.l * s < l * b.s;
}
}a[maxn];
int dp[maxn];
int main()
{
int t; cin >> t;
int cases = 0;
while(t--)
{
int n; cin >> n;
int tim = 0;
mem(dp, -inf);
dp[0] = 0;
for(int i = 1; i <= n; i++) cin >> a[i].s >> a[i].e >> a[i].l, tim += a[i].s;
sort(a + 1, a + 1 + n);
for(int i = 1; i <= n; i++)
for(int j = tim; j >= a[i].s; j--)
dp[j] = max(dp[j], dp[j - a[i].s] + a[i].e - (j - a[i].s) * a[i].l);
int maxx = 0;
for(int i = 1; i <= tim; i++)
maxx = max(maxx, dp[i]);
printf("Case #%d: %d\n", ++cases, maxx);
}
}