HDU3591 The trouble of Xiaoqian

这题一看 就知道 是 完全背包和多重背包的结合题目了;

对于买东西的是多重背包,商店是完全背包;

这里要用到背包的初始化问题,因为求最小,所以初值要尽量大,大于所有可能的最大值;

值得一提的是背包上限是20000,不是读入的T;

用两个数组分别记录需要用到的最少货币数,一维就够用了;

多重的单调队列好难不懂..只会用二进制优化过的多重背包;

下面给出代码:

#include<iostream>
#include<algorithm>
#define maxn  100010
#define min(a,b) (a>b?b:a)
int v[maxn],c[maxn],f[maxn],g[maxn],n,t;
using namespace std;
int main()
{
    int count=0;
    //freopen("3591.txt","r",stdin);
    while(~scanf("%d %d",&n,&t),n,t)
    {
        int i,j,k;
        for(i=0;i<n;i++)
            scanf("%d",v+i);
        for(i=0;i<n;i++)
            scanf("%d",c+i);
        for(i=1;i<maxn;i++)
            f[i]=g[i]=maxn;
        f[0]=g[0]=0;
        for(i=0;i<n;i++)
        {
            if(v[i]*c[i]>=20000)//完全背包
            {
                for(j=v[i];j<=20000;j++)
                    f[j]=min(f[j],f[j-v[i]]+1);
                continue;
            }
            k=1;
            while(k<c[i])//0-1 背包
            {
                for(j=20000;j>=k*v[i];j--)
                    f[j]=min(f[j],f[j-k*v[i]]+k);
                c[i]-=k;
                k*=2;
            }
            for(j=20000;j>=c[i]*v[i];j--)
                f[j]=min(f[j],f[j-c[i]*v[i]]+c[i]);
        }
        for(i=0;i<n;i++)
          for(j=v[i];j<=20000;j++)
                g[j]=min(g[j],g[j-v[i]]+1);
        int ans=maxn;
        for(i=t;i<=20000;i++)
        {
            if(ans>f[i]+g[i-t])
                ans=f[i]+g[i-t];
        }
        if(ans==maxn) ans=-1;
        printf("Case %d: %d\n",++count,ans);
    }
    return 0;
}

HDU3591 The trouble of Xiaoqian,布布扣,bubuko.com

时间: 2025-01-19 22:03:50

HDU3591 The trouble of Xiaoqian的相关文章

hdu 3591 The trouble of Xiaoqian

hdu 3591  The trouble of Xiaoqian 题意:xiaoqi要买一个T元的东西,当前的货币有N种,xiaoqi对于每种货币有Ci个:题中定义了最小数量即xiaoqi拿去买东西的钱的张数加上店家找的零钱的张数(店家每种货币有无限多张,且找零是按照最小的数量找零的):问xiaoqi买元东西的最小数量? 多重背包+完全背包: 思路:这个最小数量是拿去买东西的张数和找零的张数之和,其实我们只需要将这两个步骤分开,开算出能买T元东西的前i最少f[i]张,这里涉及到容量问题:容量只

HDU 3591 The trouble of Xiaoqian(多重背包+01背包)

HDU 3591 The trouble of Xiaoqian(多重背包+01背包) http://acm.hdu.edu.cn/showproblem.php?pid=3591 题意: 有一个具有n种货币的货币系统, 每种货币的面值为val[i]. 现在小杰手上拿着num[1],num[2],-num[n]个第1种,第2种-第n种货币去买价值为T(T<=20000)的商品, 他给售货员总价值>=T的货币,然后售货员(可能,如果小杰给的钱>T, 那肯定找钱)找钱给他. 售货员每次总是用

【背包专题】C - The trouble of Xiaoqian hdu3591【混合背包:多重背包+完全背包】

In the country of ALPC , Xiaoqian is a very famous mathematician. She is immersed in calculate, and she want to use the minimum number of coins in every shopping. (The numbers of the shopping include the coins she gave the store and the store backed

hdu3591The trouble of Xiaoqian 多重背包+完全背包

//给出Xiaoqian的钱币的价值和其身上有的每种钱的个数 //商家的每种钱的个数是无穷,xiaoqian一次最多付20000 //问怎样付钱交易中钱币的个数最少 //Xiaoqian是多重背包 //商家是完全背包 #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn = 20010 ; const int inf = 0x3f3f3f3f

hdu 3591 The trouble of Xiaoqian(多重背包)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> using namespace std; int v[200],c[200]; int dp[30000]; int main() { int n,t; int i,j,k; int cas=1; while(scanf("%d%d",&n

HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)

http://acm.hdu.edu.cn/showproblem.php? pid=3591 题意: 有一个具有n种货币的货币系统, 每种货币的面值为val[i]. 如今小杰手上拿着num[1],num[2],-num[n]个第1种,第2种-第n种货币去买价值为T(T<=20000)的商品, 他给售货员总价值>=T的货币,然后售货员(可能,假设小杰给的钱>T,那肯定找钱)找钱给他. 售货员每次总是用最少的硬币去找钱给小杰. 如今的问题是: 小杰买价值T的商品时, 他给售货员的硬币数目+

HDU-3591 混合背包

The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2277    Accepted Submission(s): 805 Problem Description In the country of ALPC , Xiaoqian is a very famous mathematician.

hdu 3591 多重加完全DP

题目: The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1997    Accepted Submission(s): 711 Problem Description In the country of ALPC , Xiaoqian is a very famous mathematici

HDU_3591_(多重背包+完全背包)

The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2006    Accepted Submission(s): 712 Problem Description In the country of ALPC , Xiaoqian is a very famous mathematician.