HDU 2126 (背包方法数) Buy the souvenirs

DP还有很长很长一段路要走。。

题意:给出n纪念品的价格和钱数m,问最多能买多少件纪念品和买这些数量的纪念品的方案数。

首先,求能买最多的纪念品的数量,用贪心法可以解决。将价钱排序,然后从最便宜的开始买,这样就很容易求得最多买的纪念品的数量。

方案数就要用到动态规划。

 

dp[j][k]表示花费不超过j元买k件物品的方案数

dp[j][k] += dp[j-a[i]][k-1]

因为这里本来是个三维数组的,多一个维度用来表示前i件物品。调整了循环顺序,类似01背包空间上的优化,所以倒着循环就可以利用之前的计算结果节省空间。

 

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 int dp[8][4], a[505];
 9 
10 int main(void)
11 {
12     #ifdef LOCAL
13         freopen("2126in.txt", "r", stdin);
14     #endif
15 
16     int T;
17     scanf("%d", &T);
18     while(T--)
19     {
20         int n, m, num = 0, sum = 0;
21         scanf("%d%d", &n, &m);
22         for(int i = 1; i <= n; ++i)
23             scanf("%d", &a[i]);
24 
25         sort(a+1, a+1+n);
26         for(int i = 1; i <= n; ++i)
27         {
28             sum += a[i];
29             if(m >= sum)
30                 ++num;
31         }
32         if(num == n)
33         {
34             printf("You have 1 selection(s) to buy with %d kind(s) of souvenirs.\n", num);
35             continue;
36         }
37         if(num == 0)
38         {
39             printf("Sorry, you can't buy anything.\n");
40             continue;
41         }
42         memset(dp, 0, sizeof(dp));
43         for(int i = 0; i <= m; ++i)
44             dp[i][0] = 1;
45         for(int i = 1; i <= n; ++i)
46             for(int j = m; j >= a[i]; --j)
47                 for(int k = num; k >= 1; --k)
48                     dp[j][k] = dp[j][k] + dp[j-a[i]][k-1];
49 
50         if(dp[m][num] == 0)
51             printf("Sorry, you can't buy anything.\n");
52         else
53             printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n", dp[m][num], num);
54     }
55     return 0;
56 }
代码君

 

 解法二:

看注释吧。。

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 int a[35];
 8 int dp[35][505][2];    //dp[i][j][0]表示前i件物品花费不超过j元
 9                     //能最多买的礼物数,dp[i][j][1]表示方案数
10 
11 int main(void)
12 {
13     #ifdef LOCAL
14         freopen("2126in.txt", "r", stdin);
15     #endif
16 
17     int T;
18     scanf("%d", &T);
19     while(T--)
20     {
21         int n, m;
22         scanf("%d%d", &n, &m);
23         for(int i = 1; i <= n; ++i)
24             scanf("%d", &a[i]);
25         memset(dp, 0, sizeof(dp));
26         //边界初始化
27         for(int i = 0; i <= n; ++i)
28             dp[i][0][1] = 1;
29         for(int j = 0; j <= m; ++j)
30             dp[0][j][1] = 1;
31         for(int i = 1; i <= n; ++i)
32         {
33             for(int j = 0; j <= m; ++j)
34             {
35                 if(j < a[i] || dp[i-1][j-a[i]][0]+1 < dp[i-1][j][0])
36                 {//如果买第i件礼物礼物总数变少,那么肯定不买!
37                     dp[i][j][0] = dp[i-1][j][0];
38                     dp[i][j][1] = dp[i-1][j][1];
39                 }
40                 else if(dp[i-1][j-a[i]][0] + 1 == dp[i-1][j][0])
41                 {//买的礼物数相同,则方案数为买和不买第i件礼物的方案数的总和
42                     dp[i][j][0] = dp[i-1][j][0];
43                     dp[i][j][1] = dp[i-1][j][1] + dp[i-1][j-a[i]][1];
44                 }
45                 else    //dp[i-1][j-a[i]][0] + 1 > dp[i-1][j][0]
46                 {//可以买更多数量的礼物
47                     dp[i][j][0] = dp[i-1][j-a[i]][0] + 1;
48                     dp[i][j][1] = dp[i-1][j-a[i]][1];
49                 }
50             }
51         }
52 
53         if(dp[n][m][0])
54             printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n", dp[n][m][1], dp[n][m][0]);
55         else
56             printf("Sorry, you can't buy anything.\n");
57     }
58     return 0;
59 }
代码君

 

解法二空间上的优化之滚动数组:

对了,那个else不能去掉!血一般的教训

 1 #define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 int a[35];
 8 int dp[505][2];
 9 
10 int main(void)
11 {
12     #ifdef LOCAL
13         freopen("2126in.txt", "r", stdin);
14     #endif
15 
16     int T;
17     scanf("%d", &T);
18     while(T--)
19     {
20         int n, m;
21         scanf("%d%d", &n, &m);
22         for(int i = 1; i <= n; ++i)
23             scanf("%d", &a[i]);
24         memset(dp, 0, sizeof(dp));
25         for(int i = 0; i <= m; ++i)    //处理边界
26             dp[i][1] = 1;
27         for(int i = 1; i <= n; ++i)
28         {
29             dp[0][1] = 1;
30             for(int j = m; j >= a[i]; --j)
31             {
32                 if(dp[j-a[i]][0] + 1 > dp[j][0])
33                 {
34                     dp[j][0] = dp[j-a[i]][0] + 1;
35                     dp[j][1] = dp[j-a[i]][1];
36                 }
37                 else if(dp[j-a[i]][0] + 1 == dp[j][0])
38                 {
39                     dp[j][1] = dp[j][1] + dp[j-a[i]][1];
40                 }
41             }
42         }
43         if(dp[m][0])
44             printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n", dp[m][1], dp[m][0]);
45         else
46             printf("Sorry, you can't buy anything.\n");
47     }
48     return 0;
49 }
代码君

 

 最后吐槽一下我发现的一个奇怪的现象:如果源代码的文件名里带括号,这样下断点调试的时候会自动退出的。CFree-5和DevCpp都是这样的,=_=||

 

posted @ 2014-08-12 08:29  AOQNRMGYXLMV  阅读(216)  评论(0编辑  收藏  举报