总结需要有一个头图:
2025.11.16课程知识要点+题目解析+总结
知识要点:背包DP
完全背包:每种物品都有无限个
01背包:每种商品都只有一个
多重背包:每种商品都有数量限制
分组背包:例如有若干个商店,每个商店有很多玩具,每个商店最多买一个玩具。
题目解析+总结
T692108 落落的凑钱问题 1
分析题干+步骤书写
状态定义: : 刚好凑齐块钱的方案数 初始化: 凑块钱需要张 转移方程 (从开始):
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,dp[5005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>t;
if(t == 0) cout<<1<<endl;
else{
dp[1] = 1,dp[2] = 2;
for(int i=3;i<=t;++i) dp[i] = (dp[i-1]+dp[i-2])%100003;
cout<<dp[t]<<endl;
}
return 0;
}T692108 落落的凑钱问题 2
分析题干+步骤书写
状态定义: :刚好凑齐块钱的方案数 初始化: 凑 块钱需要 张 转移方程:
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,k,dp[5005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>t>>k;
if(t == 0) cout<<1<<endl;
else{
dp[0] = 1;
for(int i=1;i<=t;++i){
for(int j=1;j<=min(k,i);++j) dp[i] = (dp[i] + dp[i-j]) % 100003;
}
cout<<dp[t]<<endl;
}
return 0;
}T692108 落落的凑钱问题 3
分析题干+步骤书写
这道题和上一题的区别:纸币的金额从 ~ 变成了 ~ 则转移方程变为:
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,a[101],dp[5005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>t>>n;
for(int i=1;i<=n;++i) cin>>a[i];
if(t == 0) cout<<-1<<endl;
else{
dp[0] = 1;
for(int i=1;i<=t;++i){
for(int j=1;j<=n;++j){
int x = i-a[j];
if(x >= 0) dp[i] = (dp[i] + dp[x]) % 100003;
}
}
cout<<dp[t]<<endl;
}
return 0;
}T692108 落落的凑钱问题 4
分析题干+步骤书写
状态定义: : 刚好凑齐 块钱需要的最少纸币数量 初始化: fill(dp, dp + t + 1, 1e9) 初始化:一开始都需要无穷多纸币 凑 块钱需要 张 转移方程:
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,dp[5005],a[101];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>t>>n;
for(int i=1;i<=n;++i) cin>>a[i];
if(t == 0) cout<<1<<endl;
else{
fill(dp+1,dp+1+t,1e9);
dp[0] = 0;
for(int i=1;i<=t;++i){
for(int j=1;j<=n;++j){
int x = i-a[j];
if(x >= 0) dp[i] = min(dp[i],dp[x]+1);
}
}
if(dp[t] == 1e9) cout<<-1<<endl;
else cout<<dp[t]<<endl;
}
return 0;
}T692108 落落的凑钱问题 5
分析题干+步骤书写
上一题,每选择一张纸币,代价都是 次胖揍。 这一题,每选择一张纸币,代价都是 次胖揍。 则定义为刚好凑到 块钱的最小代价。
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,dp[5005],a[101],b[101];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>t>>n;
for(int i=1;i<=n;++i) cin>>a[i]>>b[i];
if(t == 0) cout<<1<<endl;
else{
fill(dp+1,dp+1+t,1e9);
dp[0] = 0;
for(int i=1;i<=t;++i){
for(int j=1;j<=n;++j){
int x = i-a[j];
if(x >= 0) dp[i] = min(dp[i],dp[x]+b[j]);
}
}
if(dp[t] == 1e9) cout<<-1<<endl;
else cout<<dp[t]<<endl;
}
return 0;
}T692732 落落钱凑够了要买玩具 1
分析题干+步骤书写
状态定义: : 有 块钱时最大的快乐值。(注:不用刚好花完) 初始化: fill(dp, dp + n + 1, 0) 一开始都没有玩具 凑 块钱需要 张 转移方程:
注意:有 块钱的快乐肯定大于等于有 块钱的快乐,所以要注意加个凑不到 块钱的时候,那就只能继承 块钱的快乐。
如果要求不能浪费:可以加个判断:
if(i-a[j] > 0 && dp[i-a[j]] == 0) continue;
说明浪费了,但是 是合理的。
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,dp[10000005],a[10001],b[10001];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>t>>n;
for(int i=1;i<=n;++i) cin>>a[i]>>b[i];
dp[0] = 0;
for(int i=1;i<=t;++i){
for(int j=1;j<=n;++j){
int x = i-a[j];
if(x >= 0) dp[i] = max(dp[i],dp[x]+b[j]);
}
}
cout<<dp[t]<<endl;
return 0;
}T692732 落落钱凑够了要买玩具 2
分析题干+步骤书写
: 有 块钱时的最大快乐值。 因为每件物品只有一个,所以必须用外层枚举物品,内容枚举时间的写法一件件的更新 DP 值。
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,dp[10000005],a[10001],b[10001];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>t>>n;
for(int i=1;i<=n;++i) cin>>a[i]>>b[i];
dp[0] = 0;
for(int i=1;i<=n;++i){
for(int j=t;j>=1;--j){
int x = j-a[i];
if(x >= 0) dp[j] = max(dp[j],dp[x]+b[i]);
}
}
cout<<dp[t]<<endl;
return 0;
}T692732 落落钱凑够了要买玩具 3
分析题干+步骤书写
表示有 块钱时的最大快乐值。 转移方程:
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,dp[10000005],a[10001],c[10001],b[10001];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>t>>n;
for(int i=1;i<=n;++i) cin>>b[i]>>a[i]>>c[i];
dp[0] = 0;
for(int i=1;i<=n;++i){
for(int j=t;j>=1;--j){
for(int k=0;k<=c[i];++k){
int x = j-a[i]*k;
if(x >= 0) dp[j] = max(dp[j],dp[x]+b[i]*k);
}
}
}
cout<<dp[t]<<endl;
return 0;
}T692732 落落钱凑够了要买玩具 4
AC代码
#include<bits/stdc++.h>
using namespace std;
int t,n,a[1005],b[1005],c[1005],num[1005],g[105][1005],dp[1005];
int main(){
cin>>t>>n;
int maxx = 0;
for(int i=1;i<=n;++i){
cin>>a[i]>>b[i]>>c[i];
maxx = max(maxx,c[i]),num[c[i]]++,g[c[i]][num[c[i]]] = i;
}
for(int i=1;i<=maxx;++i){ //枚举组数
for(int j=t;j>=1;--j){ //枚举钱数(逆序)
for(int k=1;k<=num[i];++k){ //最后第i组的物品,因为每组只能选一个
int x = j-a[g[i][k]];// a[g[i][k]]:第i组的第k个玩具的价格
if(x >= 0) dp[j] = max(dp[j],dp[x]+b[g[i][k]]);
}
}
}
cout<<dp[t]<<endl;
return 0;
}