先导知识
先是贴一下洛谷的背包九讲
如果先导知识不会可以看(其实我就是自己贴出来给自己看的)
来写一篇背包总结,以及自己找bug的痛苦 快乐时光
01背包
01背包的核心代码,核心状态方程,即每个物品的状态只有存在或不存在两种状态,而决定存在的是背包大小和背包的最大价值。
f[i][v]=max(f[i-1][v],f[i-1][v-c[i]]+w[i]);
在背包九讲里有相关伪代码和推导过程,不再赘述。
P1048 [NOIP2005 普及组] 采药
本蒟蒻第一次接触,照着背包九讲的状态方程写出来的,可以是理想中的板子题
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
const int maxn=200;
int t,m;
int w[maxn],val[maxn];
int dp[110][1010];
int main()
{
scanf("%d%d",&t,&m);
for(int i=1;i<=m;++i)
scanf("%d%d",&w[i],&val[i]);
for(int i=1;i<=m;++i)
{
for(int j=t;j>=0;--j)
{
if(j>=w[i])
dp[i][j]=max(dp[i-1][j-w[i]]+val[i],dp[i-1][j]);
else
dp[i][j]=dp[i-1][j];
}
}
printf("%d",dp[m][t]);
return 0;
}
P2871 [USACO07DEC]Charm Bracelet S
这道题和采药那道题像的一塌糊涂,我一开始直接用二阶矩阵,但是mle和wa,之后就学会了一维的01背包。
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
const int maxn=200;
long n,m;
int w,val;
int dp[20000];
int main()
{
scanf("%ld%ld",&n,&m);
for(int i=1;i<=n;++i)
{
scanf("%d%d",&w,&val);
for(int j=m;j>0;--j)
{
if(w<=j)
dp[j]=max(dp[j],dp[j-w]+val);
}
}
printf("%d",dp[m]);
return 0;
}
01背包问题的变种,w和v一样,重量和价值一样,很容易想明白,这道题要求的是最小,就是输出的时候求个补就行了
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
const int maxn=40;
int v,n;
int w,dp[30000];
int main()
{
scanf("%d%d",&v,&n);
for(int i=1;i<=n;++i)
{
scanf("%d",&w);
for(int j=v;j>=0;--j)
{
if(j>=w)
dp[j]=max(dp[j],dp[j-w]+w);
}
}
cout << v-dp[v];
return 0;
}
01背包,注意价值 v v v的形式就行
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
const int maxn=3e4+10;
int n,m;
int v,p;
long dp[maxn];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i)
{
scanf("%d%d",&v,&p);
for(int j=n;j>=0;--j)
{
if(j>=v)
dp[j]=max(dp[j],dp[j-v]+v*p);
}
}
cout <<dp[n];
return 0;
}
01背包的计数问题,把啊 d p [ 0 ] = 1 dp[0]=1 dp[0]=1设置好就行了
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
int n,m;
int a[200],dp[10010];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
scanf("%d",&a[i]);
dp[0]=1;
for(int i=1;i<=n;++i)
{
for(int j=m;j>=a[i];--j)
dp[j]=dp[j]+dp[j-a[i]];
}
cout << dp[m];
return 0;
}
这道题的01背包十分明显,有体力,有体积,就是求最多的剩余体力时,可能比较陌生
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
const int maxn=1e4+10;
int v,n,c;
int k,m;
int dp[maxn];
int main()
{
scanf("%d%d%d",&v,&n,&c);
for(int i=0;i<n;++i)
{
scanf("%d%d",&k,&m);
for(int j=c;j>=0;--j)
{
if(j>=m)
dp[j]=max(dp[j],dp[j-m]+k);
}
}
if(dp[c]<v)
{
cout << "Impossible";
return 0;
}
for(int i=0;i<=c;++i)
{
if(dp[i]>=v)
{
cout << c-i;
break;
}
}
return 0;
}
P2639 [USACO09OCT]Bessie’s Weight Problem G
没啥说的,明显01背包
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
long h;
int n;
long t;
long dp[46000];
int main()
{
scanf("%ld %d",&h,&n);
for(int i=1;i<=n;++i)
{
scanf("%ld",&t);
for(int j=h;j>0;--j)
{
if(j>=t)
dp[j]=max(dp[j],dp[j-t]+t);
}
}
printf("%ld",dp[h]);
return 0;
}
P2925 [USACO08DEC]Hay For Sale S
背包板子题
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
const int maxn=5e4+10;
long c,h;
long dp[maxn];
long t;
int main()
{
scanf("%ld%ld",&c,&h);
for(int i=0;i<h;++i)
{
scanf("%ld",&t);
for(int j=c;j>0;--j)
{
if(j>=t)
dp[j]=max(dp[j],dp[j-t]+t);
}
}
cout << dp[c];
return 0;
}
拥有的药比需要的多,可以选择打赢或者打输,但是药不够的情况下,只能输。可能01背包的存在方式,第一种情况比较容易想到,但是第二种情况可能漏掉。
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
const int maxn=1e3+10;
int n,x;
int dp[maxn];
int main()
{
scanf("%d%d",&n,&x);
int t1,t2,t3;
for(int i=1;i<=n;++i)
{
scanf("%d %d %d",&t1,&t2,&t3);
for(int j=x;j>=0;--j)
{
if(j>=t3)
dp[j]=max(dp[j]+t1,dp[j-t3]+t2);
else if(j<t3)
dp[j]+=t1;
}
}
printf("%lld",dp[x]*5);
return 0;
}
这道题要先打个表,打完表之后十分简单
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
int rec[1007];
int dp[1007];
int main()
{
rec[1]=0;
for(int i=2;i<=1000;++i)
{
rec[i]=1;
for(int j=2;j*j<=i;++j)
{
if(i%j==0)
{
rec[i]+=j;
if(j!=i/j)
rec[i]+=(i/j);
}
}
}
int s;
scanf("%d",&s);
for(int i=1;i<=s;++i)
{
for(int j=s;j>0;--j)
{
if(j>=i)
dp[j]=max(dp[j],dp[j-i]+rec[i]);
}
}
cout << dp[s];
return 0;
}
这道题和精卫填海那道题挺像的,就是求能够最多能够剩下的,然后就是一个小贪心。
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
const int maxn=100;
int n,m,k,r;
int ts[maxn],zs[maxn],zf[maxn];
int dp[1000];
int main()
{
scanf("%d%d%d%d",&n,&m,&k,&r);
for(int i=0;i<n;++i)
scanf("%d",&ts[i]);
sort(ts,ts+n);
for(int i=1;i<=m;++i)
scanf("%d",&zs[i]);
for(int i=1;i<=m;++i)
scanf("%d",&zf[i]);
for(int i=1;i<=m;++i)
{
for(int j=r;j>=zs[i];--j)
dp[j]=max(dp[j],dp[j-zs[i]]+zf[i]);
}
int rec,cnt=0;
for(int i=0;i<=r;++i)
{
if(dp[i]>=k)
{
rec=r-i;
break;
}
}
for(int i=0;i<n;++i)
{
rec-=ts[i];
if(rec<0)
break;
else
cnt++;
}
cout << cnt;
return 0;
}
01背包的限制条件有两个,但是还是标准的01背包,对 d p [ ] [ ] dp[][] dp[][]的更新进一步优化
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
int v,m;
int n;
int vr[100],mr[100],kr[100];
int dp[510][510];
int main()
{
scanf("%d %d",&v,&m);
scanf("%d",&n);
int t1,t2,t3;
for(int i=1;i<=n;++i)
{
scanf("%d%d%d",&t1,&t2,&t3);
for(int j=v;j>=t1;--j)
for(int k=m;k>=t2;--k)
dp[j][k]=max(dp[j][k],dp[j-t1][k-t2]+t3);
}
cout << dp[v][m];
return 0;
}
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
int n,m,x;
int dp[1010][1010];
int main()
{
scanf("%d%d%d",&n,&m,&x);
int t1,t2,t3;
for(int i=1;i<=n;++i)
{
scanf("%d%d%d",&t1,&t2,&t3);
for(int j=m;j>=t2;--j)
for(int k=x;k>=t3;--k)
dp[j][k]=max(dp[j][k],dp[j-t2][k-t3]+t1);
}
cout << dp[m][x];
return 0;
}
两个限制条件的01背包,卡了个数据,dp数组开大一点
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
int n,m,t;
int dp[1000][1000];
int main()
{
scanf("%d%d%d",&n,&m,&t);
int t1,t2;
for(int i=0;i<n;++i)
{
scanf("%d%d",&t1,&t2);
for(int j=m;j>=t1;--j)
for(int k=t;k>=t2;--k)
dp[j][k]=max(dp[j][k],dp[j-t1][k-t2]+1);
}
cout << dp[m][t];
return 0;
}
完全背包
完全背包,和01背包相比有点类似,但是就是每一个物体的数量变成了无限多个,更新的方式也发生了改变
更新方式由小到大更新,和01背包的从大到小的更新方式不一样
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
const int maxn=1e4+10,maxn1=1e7+10;
long t,m;
LL a[maxn],b[maxn],dp[maxn1];
int main()
{
scanf("%ld%ld",&t,&m);
for(int i=1;i<=m;++i)
scanf("%lld%lld",&a[i],&b[i]);
for(int i=1;i<=m;++i)
for(int j=a[i];j<=t;++j)
dp[j]=max(dp[j],dp[j-a[i]]+b[i]);
printf("%lld",dp[t]);
return 0;
}
P2722 [USACO3.1]总分 Score Inflation
完全背包板子题,没啥说的
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>
typedef unsigned long long ULL;
typedef long long LL;
typedef long L;
const int maxn=1e4+10,maxn1=1e8+10;
LL m,n;
LL p[maxn],t[maxn],dp[maxn1];
int main()
{
scanf("%lld%lld",&m,&n);
for(int i=1;i<=n;++i)
scanf("%lld%lld",&p[i],&t[i]);
for(int i=1;i<=n;++i)
for(int j=t[i];j<=m;++j)
dp[j]=max(dp[j],dp[j-t[i]]+p[i]);
printf("%lld",dp[m]);
return 0;
}