洛谷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.
输入输出样例
说明
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[<<],*p1=buf,*p2=buf;
//#define int long long
using namespace std;
const int MAXN=*1e6+,INF=1e8+;
inline int read() {
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int f[MAXN];//恰好为i时的最小花费
int g[MAXN];//完全背包
int val[MAXN],num[MAXN];
int N,T,limit=;
int main() {
#ifdef WIN32
freopen("a.in","r",stdin);
#endif
N=read();T=read();
for(int i=;i<=N;i++) val[i]=read();
for(int i=;i<=N;i++) num[i]=read(),limit+=val[i]*val[i];
memset(f,0x3f,sizeof(f));
memset(g,0x3f,sizeof(g));
g[]=;f[]=;
for(int i=;i<=N;i++)
for(int j=val[i];j<=limit;j++)
g[j]=min(g[j],g[j-val[i]]+);
for(int i=;i<=N;i++) {
for(int k=;k<=num[i];k<<=) {
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 ;
}
洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)的更多相关文章
- [洛谷P2852] [USACO06DEC]牛奶模式Milk Patterns
洛谷题目链接:[USACO06DEC]牛奶模式Milk Patterns 题目描述 Farmer John has noticed that the quality of milk given by ...
- POJ 3260 The Fewest Coins 最少硬币个数(完全背包+多重背包,混合型)
题意:FJ身上有各种硬币,但是要买m元的东西,想用最少的硬币个数去买,且找回的硬币数量也是最少(老板会按照最少的量自动找钱),即掏出的硬币和收到的硬币个数最少. 思路:老板会自动找钱,且按最少的找,硬 ...
- 洛谷P2854 [USACO06DEC]牛的过山车Cow Roller Coaster
P2854 [USACO06DEC]牛的过山车Cow Roller Coaster 题目描述 The cows are building a roller coaster! They want you ...
- 洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...
- 洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...
- 洛谷 P2850 [USACO06DEC]虫洞Wormholes 题解
P2850 [USACO06DEC]虫洞Wormholes 题目描述 While exploring his many farms, Farmer John has discovered a numb ...
- 洛谷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 ...
- 洛谷 P2850 [USACO06DEC]虫洞Wormholes 判负环
虫洞(wormhole) FJ 在农场上闲逛时,发现他的农场里有很多虫洞.虫洞是一条特殊的有向路径,当 FJ 从它的一头走到另一头后,他将被传送到过去的某个时刻.FJ 的每个农场包括 N(1<= ...
- 洛谷P3726 [AH2017/HNOI2017]抛硬币(组合数+扩展Lucas)
题面 传送门 题解 果然--扩展\(Lucas\)学了跟没学一样-- 我们先考虑\(a=b\)的情况,这种情况下每一个\(A\)胜的方案中\(A\)和\(B\)的所有位上一起取反一定是一个\(A\)败 ...
随机推荐
- pycharm,右键执行run unittests in xx.py后,__main__:后的代码没执行
如图所示:执行py文件后,打印__name__的名是模块名,而非__main__ 查了好久,发现这个问题跟unittest这个类有关系,执行单元测试的py脚本时,不要右键run unittest,在p ...
- 数据库Day3之SQL Server 触发器
最近在做一个人事管理系统写了几个简单的触发器 1.在删除员工信息表中员工信息时结果区提示被删除员工信息 create trigger teston 员工信息表after deleteasselect ...
- Day 23 类的继承,派生,组合,菱形继承,多态与多态性
类的继承 继承是一种新建类的方式,新建的类称为子类,被继承的类称为父类 继承的特性是:子类会遗传父类的属性 继承是类与类之间的关系 为什么用继承 使用继承可以减少代码的冗余 对象的继承 python中 ...
- PAT_A1034#Head of a Gang
Source: PAT A1034 Head of a Gang (30 分) Description: One way that the police finds the head of a gan ...
- C#那20道题
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- code runner运行终端的目录设置
我的github:swarz,欢迎给老弟我++星星 该设置属性为 "code-runner.fileDirectoryAsCwd": true 设置为 true后,终端默认目录为运 ...
- IDEA全局查找快捷键
双击shift
- 【2】Django安装
**万物负阴而抱阳,冲气以为和 ** ——老子<道德经> 我们静下心态,开始我们的Django之旅 本节内容 Django的安装 安装结果验证 了解官方文档 1. 安装Django 我们强 ...
- ecshop ad调用指定广告的方法 邓士鹏
在include/lib_goods.php文件下面新增:function getads($cat,$num){$time = gmtime();$sql = "SELECT * FRO ...
- 【hihocoder 1499】A Box of Coins
[题目链接]:http://hihocoder.com/problemset/problem/1499 [题意] [题解] 贪心,模拟; 从左往右对于每一列; 如果上下两个格子; ① 有一个格子超过了 ...