此处有目录↑
Buy the souvenirs
Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)And you have only 7 RMB, this time you can select any combination with 3 kinds of souvenirs at most, so the selections of 3 kinds of souvenirs are ABC (6), ABD (7). But if you have 8 RMB, the selections with the most kinds of souvenirs are ABC (6), ABD (7), ACD (8), and if you have 10 RMB, there is only one selection with the most kinds of souvenirs to you: ABCD (10).
In each case, in the first line there are two integer n and m, n is the number of the souvenirs and m is the money you have. The second line contains n integers; each integer describes a kind of souvenir.
All the numbers and results are in the range of 32-signed integer, and 0<=m<=500, 0<n<=30, t<=500, and the prices are all positive integers. There is a blank line between two cases.
2 4 7 1 2 3 4 4 0 1 2 3 4
You have 2 selection(s) to buy with 3 kind(s) of souvenirs. Sorry, you can't buy anything.
题目大意:给定n个不同纪念品的价格和持有的钱,求在购买纪念品最多的情况下的方案数?
起初想到的状态:dp[i][j]表示选了i种不同的纪念品且花费为j时的方案数,最后在最大不同纪念品数上求和即可,但是没想好如何转移(没有把当前方案数保存,每次计算都是覆盖式的),结果总WA,后来看了一下题解,幡然醒悟,感觉dp还需要加强,多训练
解法一:DP O(m*n^2)
设dp[i][j]表示选了i种不同的纪念品且花费不超过j时的方案数,则状态转移方程为:dp[i][j]+=dp[i-1][j-t[k]];
便于状态转移方程的解释:设dp[k][i][j]表示前k种纪念品中选了i种不同的纪念品且花费不超过j时的方案数
进行到第k种纪念品时,dp[k][i][j]可由两个状态转移而来:①dp[k-1][i][j],即第k种纪念品不选;②dp[k-1][i-1][j-t[k]],即选取第k种纪念品
由于进行到第k种纪念品时,只与第k-1种纪念品的状态有关,所以可以像最原始的01背包进行空间上的优化
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,t[35],dp[35][505],mxKind,sum;//dp[i][j]表示选了i种不同的纪念品且花费不超过j时的方案数
int main() {
int T;
scanf("%d",&T);
while(T-->0) {
scanf("%d%d",&n,&m);
for(int k=1;k<=n;++k)
scanf("%d",t+k);
sort(t+1,t+n+1);//按照升序排序
mxKind=sum=0;
for(int k=1;k<=n;++k) {
sum+=t[k];
if(sum<=m)//寻找最大的不同纪念品数
mxKind=k;
}
memset(dp,0,sizeof(dp));
for(int k=m;k>=0;--k)//若要精确知道每个价格的方案数,只需令dp[0][0]=1即可
dp[0][k]=1;
for(int k=1;k<=n;++k) {//纪念品
for(int i=mxKind;i>=1;--i) {//选取的不同纪念品数
for(int j=m;j>=t[k];--j) {//背包容量
dp[i][j]+=dp[i-1][j-t[k]];
}
}
}
if(mxKind==0)//若最大不同纪念品数为0,则不能买任何东西
printf("Sorry, you can't buy anything.\n");
else
printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",dp[mxKind][m],mxKind);
}
return 0;
}
解法二:DP O(n*m)
又看到另一种dp解法,时空复杂度都降低了
感觉可以算是解法一的优化,对解法一中对dp[i][j](选取i种纪念品,花费不超过j时方案数)优化为way[j](花费不超过j时且选取该花费下最大的物品数时的方案数),用dp[j]表示花费不超过j时选取的最大物品数
优化正确性:优化省去了花费不超过j时且选取物品数不为最大物品数的状态,这些均为无效状态,若这些状态形成的花费不超过m时的最大物品数 为最终的最大物品数,则花费不超过j时且选取物品数且取最大物品数时能在m时转移出更大的最大物品数【感觉又没表达好...】
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,t,dp[505],way[505];//dp[i]表示花费不超过i时能选的最多物品数,way[i]表示花费不超过i时选取最多物品的情况下的方案数
int main() {
int T;
scanf("%d",&T);
while(T-->0) {
scanf("%d%d",&n,&m);
for(int i=0;i<=m;++i) {
dp[i]=0;
way[i]=1;
}
for(int k=1;k<=n;++k) {
scanf("%d",&t);
for(int j=m;j>=t;--j) {//背包容量
if(dp[j]==dp[j-t]+1)//如果花费不超过j时的最多物品数 与 花费不超过j-t时的物品数+1 相等,则选取该物品
way[j]+=way[j-t];//花费不超过j时选取最多物品数的方案数 再加上 way[j-t]
else if(dp[j]<dp[j-t]+1) {//如果花费不超过j时的最多物品数 小于 花费不超过j-t时的物品数+1
dp[j]=dp[j-t]+1;//更新花费不超过j时的最多物品数为dp[j-t]+1
way[j]=way[j-t];//花费不超过j时选取最多物品数的方案数 为 way[j-t]
}//如果花费不超过j时的最多物品数 大于 花费不超过j-t时的物品数+1,则不进行转移,因为该状态无效
}
}
if(dp[m]==0)//若最大不同纪念品数为0,则不能买任何东西
printf("Sorry, you can't buy anything.\n");
else
printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",way[m],dp[m]);
}
return 0;
}