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 ...
随机推荐
- Linux 入门教程:基础操作 01
1.1 实验内容 实验楼环境介绍 常用 Shell 命令及快捷键 Linux 使用小技巧 1.2 实验知识点 Linux 基本命令 通配符的使用 查看帮助文档 终端的概念 通常我们在使用 Linux ...
- 如何构建一个多人(.io) Web 游戏,第 1 部分
原文:How to Build a Multiplayer (.io) Web Game, Part 1 GitHub: https://github.com/vzhou842/example-.io ...
- Linux 文件查看相关的一些命令
文件压缩解压命令 # 解压 xxx.xz 并删除 xz -d test.tar.xz # 打包成 xxx.tar , 语法: tar -cvf 最后包名.tar ./要打包文件 ./要打包的文件 ta ...
- ps ww
[root@ma ~]# ps ww -p 1 PID TTY STAT TIME COMMAND 1 ? Ss 0:01 /sbin/init[root@ma ~]# ps -p 1 PID TTY ...
- 【ORA】ORA-00257 archiver error. 错误的处理方法
今天连接数据库,结果报错,ora-00257查看 [oracle@exam oracle]$ oerr ora 00257 00257, 00000, "archiver error. Co ...
- leetcode 470. 用 Rand7() 实现 Rand10() (数学,优化策略)
题目链接 https://leetcode-cn.com/problems/implement-rand10-using-rand7/ 题意: 给定一个rand7()的生成器,求解如何产生一个rand ...
- k8s用kubectl管理应用升级,服务发布与回滚,扩缩容
应用升级 Kubectl set image --help 有案例指定新版本 [root@k8s-master ~]# kubectl set image deployment/nginx nginx ...
- SQL Server management studio使用sa连接时报错与伺服器的连接已成功,但在登入程序是发生错误
使用Sql Server management studio的sa用户连接数据库时,报如下错误 解决方法: 1.使用windows验证登录 2.右键点击连接,点击属性,点击安全性,选择混合验证 3.重 ...
- Hadoop 专栏 - MapReduce 入门
MapReduce的基本思想 先举一个简单的例子: 打个比方我们有三个人斗地主, 要数数牌够不够, 一种最简单的方法可以找一个人数数是不是有54张(传统单机计算); 还可以三个人各分一摞牌数各自的(M ...
- 配置 containerd 镜像仓库完全攻略
作者简介 王海龙,Rancher中国社区技术经理,负责Rancher中国技术社区的维护和运营.拥有6年的云计算领域经验,经历了OpenStack到Kubernetes的技术变革,无论底层操作系统Lin ...