题目描述

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[<<],*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(完全背包+多重背包)的更多相关文章

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

    洛谷题目链接:[USACO06DEC]牛奶模式Milk Patterns 题目描述 Farmer John has noticed that the quality of milk given by ...

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

    题意:FJ身上有各种硬币,但是要买m元的东西,想用最少的硬币个数去买,且找回的硬币数量也是最少(老板会按照最少的量自动找钱),即掏出的硬币和收到的硬币个数最少. 思路:老板会自动找钱,且按最少的找,硬 ...

  3. 洛谷P2854 [USACO06DEC]牛的过山车Cow Roller Coaster

    P2854 [USACO06DEC]牛的过山车Cow Roller Coaster 题目描述 The cows are building a roller coaster! They want you ...

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

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

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

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

  6. 洛谷 P2850 [USACO06DEC]虫洞Wormholes 题解

    P2850 [USACO06DEC]虫洞Wormholes 题目描述 While exploring his many farms, Farmer John has discovered a numb ...

  7. 洛谷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 ...

  8. 洛谷 P2850 [USACO06DEC]虫洞Wormholes 判负环

    虫洞(wormhole) FJ 在农场上闲逛时,发现他的农场里有很多虫洞.虫洞是一条特殊的有向路径,当 FJ 从它的一头走到另一头后,他将被传送到过去的某个时刻.FJ 的每个农场包括 N(1<= ...

  9. 洛谷P3726 [AH2017/HNOI2017]抛硬币(组合数+扩展Lucas)

    题面 传送门 题解 果然--扩展\(Lucas\)学了跟没学一样-- 我们先考虑\(a=b\)的情况,这种情况下每一个\(A\)胜的方案中\(A\)和\(B\)的所有位上一起取反一定是一个\(A\)败 ...

随机推荐

  1. (转)淘淘商城系列——Solr集群搭建

    http://blog.csdn.net/yerenyuan_pku/article/details/72957201 我们之前做的搜索使用的是Solr的单机版来实现的,正是由于我们现在商品数据量不多 ...

  2. WebForm 登陆test

    <script> window.onload = function () { document.getElementById("form1").onsubmit = f ...

  3. Redis 之list链表结构及命令详解

    1.lpush  key   value   从左放一个值 2.rpush  key   value  从右放一个值 3.lrange  key  start   stop  获取链表数据(start ...

  4. Django 框架入门

    1.创建虚拟环境.(如果你想在你的服务器中运行多个项目,那么装虚拟环境是最好的选择) pip install virtualenv pip install virtualenvwrapper 安装好后 ...

  5. swiper 滑动获取当前第几页下标

  6. Nginx服务(端口80)

    Nginx安装: 一.编译安装 1.安装相应软件 yum install pcre pcre-devel openssl openssl-devel -y 2.检查: rpm -aq pcre pcr ...

  7. 09.正则表达式re-3.常用的匹配规则

    模式 描述 \w 匹配字母.数字及下划线 \W 匹配不是字母.数字及下划线的字符 \s 匹配任意空白字符,等价于[\t\n\r\f] \S 匹配任意非空字符 \d 匹配任意数字,等价于[0-9] \D ...

  8. 《你又怎么了我错了行了吧》第八次团队作业:Alpha冲刺

    项目 内容 这个作业属于哪个课程 软件工程 这个作业的要求在哪里 实验十二 团队作业8 团队名称 你又怎么了我错了行了吧 作业学习目标 (1)掌握软件测试基础技术 (2)学习迭代式增量软件开发过程,完 ...

  9. 洛谷——P1002 过河卒||codevs——T1010 过河卒

    https://www.luogu.org/problem/show?pid=1002#sub||http://codevs.cn/problem/1010/ 题目描述 棋盘上A点有一个过河卒,需要走 ...

  10. Spring MVC-表单(Form)标签-复选框集合(Checkboxes)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_checkboxes.htm 说明:示例基于Spring MVC 4.1.6. 以 ...