Buy the souvenirs HDU - 2126 01背包求最值和方案数

题意:

给出n种物品和钱数,已经n中物品的价格,求出最多可以买到的物品,以及有多少方案可以买到最多物品。

思路:

考虑到状态转移方程,dpj代表花费钱数j时所能买到的物品,cntj代表花费钱数j时有多少种买法:

即当可以买到更多物品时,更新dpj和cntj。cntj是取1和cnt[j-w[i]]的最大值的。

而当选择或不选择第i件物品最大的方案数相同时,我们合并方案数。

代码:

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int dx[10]= {-1,1,0,0};
int dy[10]= {0,0,-1,1};
const int maxn=100+10;
int dp[maxn*10];
int w[maxn];
int cnt[maxn*10];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        memset(cnt,0,sizeof(cnt));
        int n,m;
        scanf("%d %d",&n,&m);
        rep(i,1,n)scanf("%d",&w[i]);
        rep(i,1,n)
        {
            dep(j,m,w[i])
            {
                if(dp[j]<dp[j-w[i]]+1)
                {
                    dp[j]=dp[j-w[i]]+1;//计算最多可以买几个物品
                    cnt[j]=max(1,cnt[j-w[i]]);//统计方案数
                }
                else if(dp[j]==dp[j-w[i]]+1)
                {
                    cnt[j]+=max(1,cnt[j-w[i]]);
                }
            }
        }
        if(dp[m])
        {
            printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",cnt[m],dp[m]);
        }
        else
        {
            printf("Sorry, you can't buy anything.\n");
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Possible English version: Designing an Overseas Travel Plan Step 1: Choose your destination As a travel enthusiast, I have always dreamt of visiting the charming city of Paris, France. Known for its iconic landmarks, cultural heritage, and romantic atmosphere, Paris offers a perfect blend of history, art, and lifestyle that I would love to explore. Step 2: Collect basic information about the trip To plan my Parisian adventure, I need to gather some essential details, such as: - Travel time: I would prefer to go in the spring or fall, when the weather is mild and the crowds are manageable. - Travel mode: I could either fly directly from my hometown to Paris or take a connecting flight from a nearby city. Alternatively, I could take a train or a bus from another European city to reach Paris. - Accommodation: I would like to stay in a comfortable yet affordable hotel or hostel in a central location, so that I can easily access the main attractions and enjoy the local ambiance. - Budget: I need to estimate the total cost of the trip, including transportation, lodging, food, tickets, souvenirs, and miscellaneous expenses. I also need to allocate some extra money for unexpected events or emergencies. Step 3: Organize and analyze the plan Based on the above information, I can create a rough itinerary for my Paris trip, which might look like this: Day 1: Arrival in Paris, check-in at the hotel, stroll around the neighborhood, have a French dinner Day 2: Visit the Eiffel Tower, the Champs-Elysées, the Arc de Triomphe, and the Tuileries Garden Day 3: Explore the Louvre Museum, the Notre-Dame Cathedral, the Sainte-Chapelle, and the Latin Quarter Day 4: Take a day trip to Versailles Palace and Gardens, enjoy a picnic or a bike ride Day 5: Discover the Montmartre neighborhood, the Sacré-Cœur Basilica, and the Moulin Rouge cabaret Day 6: Have a leisurely brunch, shop for souvenirs or fashion items, attend a concert or a theater show Day 7: Departure from Paris, return home or continue the journey to other destinations By following this plan, I can savor the best of Paris in a week, without feeling rushed or bored. Of course, I can always adjust or customize the plan according to my interests, preferences, and availability. For example, I might skip some of the tourist traps and seek out hidden gems, or I might join a guided tour or a local event to enhance my cultural immersion. Whatever I do, I know that my Paris trip will be a memorable and enriching experience that I will cherish for a lifetime.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值