洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)

题目描述

Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus the number of coins he receives in change is minimized. Help him to determine what this minimum number is.

FJ wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1 ≤ N ≤ 100) different coins, with values V1, V2, ..., VN (1 ≤ Vi ≤ 120). Farmer John is carrying C1 coins of value V1, C2 coins of value V2, ...., and CN coins of value VN (0 ≤ Ci ≤ 10,000). The shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner (although Farmer John must be sure to pay in a way that makes it possible to make the correct change).

农夫John想到镇上买些补给。为了高效地完成任务,他想使硬币的转手次数最少。即使他交付的硬 币数与找零得到的的硬币数最少。 John想要买T(1<=T<=10000)样东西(2017-7-20 管理员注:这个翻译有问题,实际为要买价值为T的东西)。有N(1<=n<=100)种货币参与流通,面值分别为V1,V2..Vn (1<=Vi<=120)。John有Ci个面值为Vi的硬币(0<=Ci<=10000)。我们假设店主有无限多的硬币, 并总按最优方案找零。

输入输出格式

输入格式:

Line 1: Two space-separated integers: N and T.

Line 2: N space-separated integers, respectively V1, V2, ..., VN coins (V1, ...VN)

Line 3: N space-separated integers, respectively C1, C2, ..., CN

输出格式:

Line 1: A line containing a single integer, the minimum number of coins involved in a payment and change-making. If it is impossible for Farmer John to pay and receive exact change, output -1.

输入输出样例

输入样例#1: 复制

3 70
5 25 50
5 2 1

输出样例#1: 复制

3

说明

Farmer John pays 75 cents using a 50 cents and a 25 cents coin, and receives a 5 cents coin in change, for a total of 3 coins used in the transaction.

思路比较简单

对john做一次多重背包

对店主做一次完全背包(然而不会写代码)

多重背包用二进制优化

另外,本蒟蒻不怎么懂为什么枚举上界是所有面值相乘再加T,

刚开始写面值乘数量死活RE QWQ...

#include<cstring>
#include<cstdio>
#include<cstdlib>
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<22,stdin),p1==p2)?EOF:*p1++)
#define min(a,b) (a<b?a:b)
#define max(a,b) (a<b?b:a)
char buf[1<<22],*p1=buf,*p2=buf;
//#define int long long
using namespace std;
const int MAXN=5*1e6+10,INF=1e8+10;
inline int read() {
    char c=getchar();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
    return x*f;
}
int f[MAXN];//恰好为i时的最小花费
int g[MAXN];//完全背包
int val[MAXN],num[MAXN];
int N,T,limit=0;
int main() {
    #ifdef WIN32
    freopen("a.in","r",stdin);
    #endif
    N=read();T=read();
    for(int i=1;i<=N;i++) val[i]=read();
    for(int i=1;i<=N;i++) num[i]=read(),limit+=val[i]*val[i];
    memset(f,0x3f,sizeof(f));
    memset(g,0x3f,sizeof(g));
    g[0]=0;f[0]=0;
    for(int i=1;i<=N;i++)
        for(int j=val[i];j<=limit;j++)
            g[j]=min(g[j],g[j-val[i]]+1);
    for(int i=1;i<=N;i++) {
        for(int k=1;k<=num[i];k<<=1) {
            for(int j=limit;j>=val[i]*k;j--)
                    f[j]=min(f[j],f[j - val[i]*k]+k);
            num[i]-=k;
        }
        if(num[i])
            for(int j=limit;j>=val[i]*num[i];j--)
                f[j]=min(f[j],f[j - val[i]*num[i]]+num[i]);
    }
    int ans=INF;
    for(int i=T;i<=limit;i++)
        ans=min(ans,f[i]+g[i-T]);
    ans==INF?printf("-1"):printf("%d",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/zwfymqz/p/8782645.html

时间: 2024-08-28 22:02:19

洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)的相关文章

[USACO06DEC]最少的硬币The Fewest Coins

题目描述 约翰在镇上买了 T 元钱的东西,正在研究如何付钱.假设有 N 种钞票,第 i 种钞票的面值为 Vi,约翰身上带着这样的钞票 Ci 张.商店老板罗伯是个土豪,所有种类的钞票都有无限张.他们有洁癖,所以希望在交易的时候,交换的钞票张数尽可能地少.请告诉约翰如何恰好付掉 T 元,而且在过程中交换的货币数量最少. 输入格式 ? 第一行:两个整数 N 和 T,1 ≤ N ≤ 100, 1 ≤ T ≤ 10000 ? 第二行:n个整数 Vi 第三行:n个整数 Ci,1 ≤ Vi ≤ 120, 0

POJ 3260 The Fewest Coins 最少硬币个数(完全背包+多重背包,混合型)

题意:FJ身上有各种硬币,但是要买m元的东西,想用最少的硬币个数去买,且找回的硬币数量也是最少(老板会按照最少的量自动找钱),即掏出的硬币和收到的硬币个数最少. 思路:老板会自动找钱,且按最少的找,硬币数量也不限,那么可以用完全背包得出组成每个数目的硬币最少数量.而FJ带的钱是有限的,那么必须用多重背包,因为掏出的钱必须大于m,那么我们所要的是大于等于m钱的硬币个数,但是FJ带的钱可能很多,超过m的很多倍都可能,那么肯定要有个背包容量上限,网上说的根据抽屉原理是m+max*max,这里的max指

洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic

P2853 [USACO06DEC]牛的野餐Cow Picnic dfs 1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,k,p[10000],can[10000]; 4 int w[1000+15][1000+15]; 5 bool vis[10000]; 6 7 void dfs(int pre) 8 { 9 for(int j=1;j<=n;j++) 10 { 11 if(w[pre][j]&&!

洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic

P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N (1 ≤ N ≤ 1,000) pastures, conveniently numbered 1...N. The pastures are connected by M (1 ≤ M ≤ 10,000) one-way path

洛谷P2853 [USACO06DEC]牛的野餐Cow Picnic

题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N (1 ≤ N ≤ 1,000) pastures, conveniently numbered 1...N. The pastures are connected by M (1 ≤ M ≤ 10,000) one-way paths (no path connects a pasture to

洛谷 P2850 [USACO06DEC]虫洞Wormholes 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=2850 [题目描述] John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N (从1..N标号)块地,并有W个虫洞(有向边).其中1<=N<=500,1<=M<=2500,1<=W<=200.

洛谷P2852 [USACO06DEC]牛奶模式Milk Patterns

题目描述 Farmer John has noticed that the quality of milk given by his cows varies from day to day. On further investigation, he discovered that although he can't predict the quality of milk from one day to the next, there are some regular patterns in th

洛谷 P1064 金明的预算方案【有依赖的分组背包】

题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行".今天一早,金明就开始做预算了,他把想买的物品分为两类:主件与附件,附件是从属于某个主件的,下表就是一些主件与附件的例子: 主件 附件 电脑 打印机,扫描仪 书柜 图书 书桌 台灯,文具 工作椅 无 如果要买归类为附件的物品,必须先买该附件所属的主件.每个主件可以有0个.1个或2个附件.附件不

洛谷P1450 [HAOI2008]硬币购物 动态规划 + 容斥原理

洛谷P1450 [HAOI2008]硬币购物 动态规划 + 容斥原理 1.首先我们去掉限制 假设 能够取 无数次 也就是说一开始把他当做完全背包来考虑 离线DP 预处理 复杂度 4*v 用f[ i ] 表示 空间 为 i 的方案数 答案ans 其实就是所有方案 - 所有超过限制的方案 限制指的就是题目中限制 某个硬币有几枚 然后所有超过限制的方案用容斥来做 所有超过限制的方案 要减 == -1 超过限制的方案 - 2 超过限制的方案 - 3 超过限制的方案 - 4 超过限制的方案 + 1和2 超