1、hdu 2126 Buy the souvenirs

  题意:给出若干个纪念品的价格,求在能购买的纪念品的数目最大的情况下的购买方案。

  思路:01背包+记录方案。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 const int maxn = 35;
 6 const int maxw = 510;
 7 int cnt[maxw];
 8 int dp[maxw];
 9 int p[maxn];
10 int main()
11 {
12     int t;
13     scanf("%d", &t);
14     while (t--)
15     {
16         int n, m;
17         scanf("%d%d", &n, &m);
18         for (int i = 0; i < n; i++) scanf("%d", p + i);
19         memset(dp, 0, sizeof(dp));
20         memset(cnt, 0, sizeof(cnt));
21         for (int i = 0; i < n; i++)
22         {
23             for (int v = m; v >= p[i]; v--)
24             {
25                 if (dp[v - p[i]]+1 > dp[v])
26                 {
27                     dp[v] = dp[v - p[i]]+1;
28                     cnt[v]=cnt[v-p[i]]==0?1:cnt[v-p[i]];
29                 }
30                 else if (dp[v - p[i]] + 1 == dp[v])
31                 {
32                     cnt[v] += cnt[v - p[i]]==0?1:cnt[v-p[i]];
33                 }
34             }
35         }
36         if (dp[m] != 0)printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n", cnt[m], dp[m]);
37         else printf("Sorry, you can't buy anything.\n");
38     }
39     return 0;
40 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-10
  • 2022-12-23
  • 2021-08-13
  • 2021-08-07
  • 2021-10-18
  • 2021-11-11
猜你喜欢
  • 2021-08-07
  • 2021-08-07
  • 2021-11-11
  • 2021-09-13
  • 2022-12-23
  • 2021-11-16
  • 2021-12-08
相关资源
相似解决方案