Description

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 V1V2, ..., VN (1 ≤ Vi ≤
120). Farmer John is carrying C1 coins of value V1C2 coins of value V2, ...., andCN 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).

Input

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

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

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

Output

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.

Sample Input

3 70
5 25 50
5 2 1

Sample Output

3

Hint

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.

这题思路是一次枚举钱数,从要买物品的价格到最大值(即上界),这里最大值为24400(查百度的,用鸽笼原理),然后用num1[m]表示买m元买家最少要用的硬币数(可以用多重背包),num2[m]表示找钱m元最少要用的硬币数(可以用完全背包,因为数量无限),然后设一个值ans,用min(ans,num1[i]+num2[i-jiage])求出最小的硬币数。

#include<stdio.h>
#include<string.h>
#define inf 88888888
int min(int a,int b){
return a<b?a:b;
}
int num1[25500],num2[25500],v[105],num[105],w[105];
int main()
{
int n,i,j,jiage,ans,k,sum;
int m=24450;
while(scanf("%d%d",&n,&jiage)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%d",&w[i]);
v[i]=1;
}
for(i=1;i<=n;i++){
scanf("%d",&num[i]);
}
for(j=1;j<=m;j++){
num1[j]=num2[j]=inf;
}
num1[0]=num2[0]=0; for(i=1;i<=n;i++){
for(j=w[i];j<=m;j++){
if(num2[j-w[i]]!=inf){
num2[j]=min(num2[j],num2[j-w[i]]+v[i]);
}
} ans=num[i]*w[i];
if(ans>=m){
for(j=w[i];j<=m;j++){
if(num1[j-w[i]]!=inf){
num1[j]=min(num1[j],num1[j-w[i]]+v[i]);
}
}
}
else{
k=1;sum=0;
while(sum+k<num[i]){
sum+=k;
for(j=m;j>=k*w[i];j--){
if(num1[j-k*w[i]]!=inf){
num1[j]=min(num1[j],num1[j-k*w[i]]+k*v[i]);
}
}
k=k*2;
}
k=num[i]-sum;
for(j=m;j>=k*w[i];j--){
if(num1[j-k*w[i]]!=inf){
num1[j]=min(num1[j],num1[j-k*w[i]]+k*v[i]);
}
}
}
}
ans=inf;
for(i=jiage;i<=m;i++){
if(num1[i]==inf || num2[i-jiage]==inf)continue;
if(ans>num1[i]+num2[i-jiage]){
ans=num1[i]+num2[i-jiage];
}
}
if(ans!=inf)
printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}

poj3260 The Fewest Coins的更多相关文章

  1. POJ3260——The Fewest Coins(多重背包+完全背包)

    The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...

  2. POJ3260:The Fewest Coins(混合背包)

    Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...

  3. POJ3260 The Fewest Coins(混合背包)

    支付对应的是多重背包问题,找零对应完全背包问题. 难点在于找上限T+maxv*maxv,可以用鸽笼原理证明,实在想不到就开一个尽量大的数组. 1 #include <map> 2 #inc ...

  4. POJ3260The Fewest Coins[背包]

    The Fewest Coins Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6299   Accepted: 1922 ...

  5. The Fewest Coins POJ - 3260

    The Fewest Coins POJ - 3260 完全背包+多重背包.基本思路是先通过背包分开求出"付出"指定数量钱和"找"指定数量钱时用的硬币数量最小值 ...

  6. POJ 3260 The Fewest Coins(多重背包+全然背包)

    POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...

  7. POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)

    题目代号:POJ 3260 题目链接:http://poj.org/problem?id=3260 The Fewest Coins Time Limit: 2000MS Memory Limit: ...

  8. (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)

    http://poj.org/problem?id=3260   Description Farmer John has gone to town to buy some farm supplies. ...

  9. POJ 3260 The Fewest Coins(多重背包问题, 找零问题, 二次DP)

    Q: 既是多重背包, 还是找零问题, 怎么处理? A: 题意理解有误, 店主支付的硬币没有限制, 不占额度, 所以此题不比 1252 难多少 Description Farmer John has g ...

随机推荐

  1. PyTorch 于 JupyterLab 的环境准备

    PyTorch 是目前主流的深度学习框架之一,而 JupyterLab 是基于 Web 的交互式笔记本环境.于 JupyterLab 我们可以边记笔记的同时.边执行 PyTorch 代码,便于自己学习 ...

  2. 前端面试准备笔记之JavaScript(03)

    01. 变量声明提升 在预解析的时候,成员变量和函数,被提升到最高的位置,方便其他程序访问. 可以先使用后声明. 只提升变量名,不提升变量值 let const 声明的变量不具有变量声明提升. // ...

  3. MariaDB(selec的使用)

      --查询基本使用 -- 查询所有列 --select * from 表名 select * from students;   --一定条件查询 select * from students whe ...

  4. 无法获取 vmci 驱动程序版本: 句柄无效。 驱动程序 vmci.sys 版本不正确。请尝试重新安装 VMware Workstation。 打开模块DevicePowerOn电源失败。

    1.别打开电源,然后到虚拟机安装文件夹内.2.找到你的虚拟机系统文件中后缀为vmx的文件,右击用记事本或者Notepad++打开.2.搜索找到vmci0.present='TRUE',字段,把true ...

  5. hook笔记②

  6. 为什么Go自带的日志默认输出到os.Stderr?

    为什么Go自带的日志默认输出到os.Stderr? - 知乎 https://www.zhihu.com/question/67629357 Note that the Go runtime writ ...

  7. 能够满足这样要求的哈希算法有很多,其中比较著名并且应用广泛的一个哈希算法,那就是MurmurHash 算法。尽管这个哈希算法在 2008 年才被发明出来,但现在它已经广泛应用到 Redis、MemCache、Cassandra、HBase、Lucene 等众多著名的软件中。

    能够满足这样要求的哈希算法有很多,其中比较著名并且应用广泛的一个哈希算法,那就是MurmurHash 算法.尽管这个哈希算法在 2008 年才被发明出来,但现在它已经广泛应用到 Redis.MemCa ...

  8. 函数式编程 偏函数 生成器 yield

    高阶函数 # 高阶函数def f(x): return x * x# map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Ite ...

  9. tcp服务器

    如同上面的电话机过程一样,在程序中,如果想要完成一个tcp服务器的功能,需要的流程如下: socket创建一个套接字 bind绑定ip和port listen使套接字变为可以被动链接 accept等待 ...

  10. Java泛型机制

    泛型程序设计 1.泛型程序设计的起源? 泛型是JDK1.5增加的新特性. 2.使用泛型的好处? 使用泛型机制编写的代码比那些杂乱使用Object变量,然后再进行强制类型转换的代码具有更好的安全性和可读 ...