poj3260 The Fewest Coins
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 V1, V2, ..., VN (1 ≤ Vi ≤
120). Farmer John is carrying C1 coins of value V1, C2 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 2: N space-separated integers, respectively V1, V2, ..., VN coins (V1, ...VN)
Line 3: N space-separated integers, respectively C1, C2, ..., CN
Output
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的更多相关文章
- POJ3260——The Fewest Coins(多重背包+完全背包)
The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...
- POJ3260:The Fewest Coins(混合背包)
Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...
- POJ3260 The Fewest Coins(混合背包)
支付对应的是多重背包问题,找零对应完全背包问题. 难点在于找上限T+maxv*maxv,可以用鸽笼原理证明,实在想不到就开一个尽量大的数组. 1 #include <map> 2 #inc ...
- POJ3260The Fewest Coins[背包]
The Fewest Coins Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6299 Accepted: 1922 ...
- The Fewest Coins POJ - 3260
The Fewest Coins POJ - 3260 完全背包+多重背包.基本思路是先通过背包分开求出"付出"指定数量钱和"找"指定数量钱时用的硬币数量最小值 ...
- POJ 3260 The Fewest Coins(多重背包+全然背包)
POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...
- POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)
题目代号:POJ 3260 题目链接:http://poj.org/problem?id=3260 The Fewest Coins Time Limit: 2000MS Memory Limit: ...
- (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)
http://poj.org/problem?id=3260 Description Farmer John has gone to town to buy some farm supplies. ...
- POJ 3260 The Fewest Coins(多重背包问题, 找零问题, 二次DP)
Q: 既是多重背包, 还是找零问题, 怎么处理? A: 题意理解有误, 店主支付的硬币没有限制, 不占额度, 所以此题不比 1252 难多少 Description Farmer John has g ...
随机推荐
- MongoDB Sharding(二) -- 搭建分片集群
在上一篇文章中,我们基本了解了分片的概念,本文将着手实践,进行分片集群的搭建 首先我们再来了解一下分片集群的架构,分片集群由三部分构成: mongos:查询路由,在客户端程序和分片之间提供接口.本次实 ...
- 根据业务摸索出的一个selenium代码模版(python)
前言 总算入行上班几个月了,不得不说业务是真的不消停啊.. 本人工作上经常遇到一种场景:为甲方做自动化接口处理工具,登录需要短信验证码,, 嘛算是摸索出了一套selenium代码模板,主要解决如下痛点 ...
- 跨站脚本漏洞(XSS)基础
什么是跨站脚本攻击XSS 跨站脚本(cross site script),为了避免与样式css混淆所以简称为XSS,是一种经常出现在web应用中的计算机安全漏洞,也是web中最主流的攻击方式. 什么是 ...
- 【Linux】实现端口转发的rinetd
Linux下端口转发一般都使用iptables来实现,使用iptables可以很容易将TCP和UDP端口从防火墙转发到内部主机上.但是如果需要将流量从专用地址转发到不在您当前网络上的机器上,可尝试另一 ...
- ctfhub技能树—文件上传—MIME绕过
什么是MIME MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型.是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访 ...
- 06--Docker自定义镜像Tomcat9
1. 创建目录 /zhengcj/mydockerfile/tomcat9 2.将jdk和tomcat的安装包拷贝到tomcat9下 3.在tomcat9目录下创建Dockerfile文件,并写以下命 ...
- Spring入门及IoC的概念
Spring入门 Spring是一个轻量级的Java开发框架,最早由Robd Johnson创建,目的为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题,它是一个分层的JavaSE/EE轻量级开源 ...
- uni-app开发经验分享三: Vuex实现登录和用户信息留存
在做用户登录的过程中,其实最重要的是登录成功后的数据要怎么储存,储存到哪里,这里我分享一个利用vuex来实现用户登录和用户数据留存的方法 vuex代码如下: //引入vue和vuex import V ...
- 使用XML作为配置表,WinForm程序读取配置表来动态显示控件
一.首先创建一个XML文件定义以下格式(uName:显示的中文字,uKey:代表控件的Name属性,ukeyValue:代表是否显示) 二.项目中定义一个通用类,来存放读取的值 这三个字段对应XML文 ...
- HA工作机制及namenode向QJM写数据流程
HA工作机制 (配置HA高可用传送门:https://www.cnblogs.com/zhqin/p/11904317.html) HA:高可用(7*24小时不中断服务) 主要的HA是针对集群的mas ...