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. Netty入门一:服务端应用搭建 & 启动过程源码分析

    最近周末也没啥事就学学Netty,同时打算写一些博客记录一下(写的过程理解更加深刻了) 本文主要从三个方法来呈现:Netty核心组件简介.Netty服务端创建.Netty启动过程源码分析 如果你对Ne ...

  2. Restful API是什么、为什么、怎么使用

    Restful API 文章目录 Restful API 1.REST是什么以及它的 6 个限制 REST是什么? REST的6个限制 2. Restful是什么 Restful是什么 RESTful ...

  3. 【Java】网络编程之NIO

    简单记录 慕课网-解锁网络编程之NIO的前世今生 & 一站式学习Java网络编程 全面理解BIO/NIO/AIO 内容概览 文章目录 1.[了解] NIO网络编程模型 1.1.NIO简介 1. ...

  4. 【Python】简单的脚本,轻松批量修改文件名称

    使用python脚本,批量修改文件夹名称 先创建一些没用的案例文件 import os #创建新文件夹 dir = os.makedirs('D:\\SomeThing\\testfile') #将文 ...

  5. 【win10】win10下两个显示器不同桌面壁纸

    win10系统下,双屏显示为不同的桌面壁纸 操作: 1.鼠标右键点击个性化 2.点击背景选项 3.在图片上右键选择要添加为背景的图片 同理,将另一个屏幕壁纸设为监视器1 最后效果为两个分屏为不同桌面壁 ...

  6. oracle dg库因为standby_file_management参数导致应用停止

    DG库的standby_file_management=manual,主库添加文件的时候,备库无法自动创建对应的文件而报错 File #154 added to control file as 'UN ...

  7. Git安装/VScode+Git+Github

    Git安装/VScode+Git+Github 1. 相关简介 git 版本控制工具,支持该工具的网站有Github.BitBucket.Gitorious.国内的OS China仓库.Csdn仓库等 ...

  8. 無法直接連接互聯網,需要使用代理時(Scrapy)

    在windows系統中,如果無法直接連接互聯網,需要使用代理時該怎麽做呢? 1. 在powershell中設置proxy 背景:使用公司電腦,無法直接訪問互聯網,想要訪問互聯網就得使用代理,但是在控制 ...

  9. jackson学习之三:常用API操作

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  10. CentOS7,非LVM根分区扩容步骤:

    1.查看现有的分区大小 非LVM分区,目前磁盘大小为40G,根分区总容量为40G,(是自定义分区安装的) 2.关机增加磁盘大小至100G 如果你们是vmwaer虚拟软件安装的那如下入扩容: 3.查看磁 ...