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, ...., 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).
Input
Line 2: N space-separated integers, respectively
V
1,
V
2, ...,
VN coins (
V
1, ...
VN)
Line 3: N space-separated integers, respectively
C
1,
C
2, ...,
CN
Output
Sample Input
3 70
5 25 50
5 2 1
Sample Output
3
题意:给出钱币的方案数和总价值,然后给出每种钱币的价值与数量,而老板也是每种钱币都拥有,但是没有数量限制,购买东西的时候,价值超过给定价值的话,老板会找钱,要求最小的交流钱币的数量
思路:这题想了很久没有想出思路,虽然知道是背包,但是不知道该如何让运用,看了别人的代码,感觉人家的思路真心碉堡了,讲解在代码中
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int v[105],c[105],MAX,n,sum;
int dp[33333],inf = 100000000; void ZeroOnePack(int cost,int cnt)
{
int i;
for(i = sum+MAX; i>=cost; i--)
dp[i] = min(dp[i],dp[i-cost]+cnt);//找出最小数量的方案
} void CompletePack(int cost,int cnt)
{
int i;
for(i = sum+MAX+cost; i>=0; i--)
dp[i] = min(dp[i],dp[i-cost]+cnt);
} int MultiplePack()
{
int i,j,k;
for(i = 1; i<=sum+MAX; i++)
dp[i] = inf;
dp[0] = 0;//dp数组用来记录钱币数量
for(i = 1; i<=2*n; i++)
{
if(i<=n)//这是顾客购买时所给的钱的数量
{
k = 1;
while(k<c[i])
{
ZeroOnePack(k*v[i],k);
c[i]-=k;
k*=2;
}
ZeroOnePack(c[i]*v[i],c[i]);
}
else
CompletePack(-v[i-n],1);//只所以是负数,是因为这是老板找钱的数目
}
if(dp[sum]==inf)
return -1;
else
return dp[sum];
} int main()
{
int i;
while(~scanf("%d%d",&n,&sum))
{
MAX = 0;
for(i=1; i<=n; i++)
{
scanf("%d",&v[i]);
MAX = max(MAX,v[i]);
}
MAX*=MAX;//保证背包足够大
for(i=1; i<=n; i++)
scanf("%d",&c[i]);
printf("%d\n",MultiplePack());
} 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 ...
- POJ 3260 The Fewest Coins(多重背包+全然背包)
POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...
- POJ3260 The Fewest Coins(混合背包)
支付对应的是多重背包问题,找零对应完全背包问题. 难点在于找上限T+maxv*maxv,可以用鸽笼原理证明,实在想不到就开一个尽量大的数组. 1 #include <map> 2 #inc ...
- 洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)
题目描述 Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always p ...
- poj3260 The Fewest Coins
Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...
- POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)
题目代号:POJ 3260 题目链接:http://poj.org/problem?id=3260 The Fewest Coins Time Limit: 2000MS Memory Limit: ...
- 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 完全背包+多重背包.基本思路是先通过背包分开求出"付出"指定数量钱和"找"指定数量钱时用的硬币数量最小值 ...
- HDU 3535 AreYouBusy (混合背包)
题意:给你n组物品和自己有的价值s,每组有l个物品和有一种类型: 0:此组中最少选择一个 1:此组中最多选择一个 2:此组随便选 每种物品有两个值:是需要价值ci,可获得乐趣gi 问在满足条件的情况下 ...
随机推荐
- c#利用VM_COPYDATA实现进程间通信
c#进程间的通信方式很多种,只会这种,感觉比较简单.不懂原理,能用就行. 假设有两个程序:server(主进程),client(子进程) 1.server端: /*定义一个结构体,用来接收从子进程传过 ...
- javascript什么是函数
函数是完成某个特定功能的一组词语.如没有函数,完成任务可能需要五行.十行.甚至更多的代码. 这是未满就可以把完成特定功能的代码块放到一个函数里,直接调用这个函数,就省重复输入大量代码的麻烦. 如何定义 ...
- memcached session共享
http://www.baidu.com/s?wd=memcached%20session%E5%85%B1%E4%BA%AB&rsv_spt=1&issp=1&f=8& ...
- Java ----------- SQL语句总结(更新中。。。。。。)
#对数据库的操作 *创建数据库 CREATE DATABASE database_name:database_name为创建的数据库的变量名称. #对表的操作
- C#基本数据类型与C++区别
与C++不同的地方: char占两个字节存Unicode字符, long long 改为 long ; unsize ... 改为 u... 新增: byte占1个字节,类似与C++char, sby ...
- C# 窗体程序入门 之计算器
之前一直在java的B/S,这次被要求做一个C/S,其中客户端主要是界面和socket通信.因为没有使用过C#和Visual Studio的控件编程,先来个HelloWorld. 我的环境是visua ...
- socket网络编程中的同步,异步,阻塞式,非阻塞式,有何联系与区别?
一.举个打电话的例子: 阻塞 block 是指,你拨通某人的电话,但是此人不在,于是你拿着电话等他回来,其间不能再用电话.同步大概和阻塞差不多. 非阻塞 nonblock 是指,你拨通 ...
- No2_5.类的高级特性_Java学习笔记_抽象类和成员内部类
一.抽象类1.所谓抽象类,只声明方法的存在而不去实现它的类:2.抽象类不能被实例化,即不能实现其对象:3.abstract class 类名{ 类体 }4.包含一个或多个抽象方法的类必须声明成抽象类: ...
- ORA-942 SP2-0611
环境:oracle 11.2.04 问题描述: 在使用hr用户启用set autot trace时报错 set">HR@test>set autot trace; Error O ...
- js delete 用法
1,对象属性删除 function fun(){ this.name = 'mm'; } var obj = new fun(); console.log(obj.name);//mm delet ...