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 问在满足条件的情况下 ...
随机推荐
- android 中文件加密 解密 算法实战
现在项目里面有一个需求,本项目里面下载的视频和文档都不允许通过其他的播放器播放,在培训机构里面这样的需求很多.防止有人交一份钱,把所有的课件就拷给了别人.这样的事情培训机构肯定是不愿意的.现在我项目里 ...
- linux下单独安装oracle12.1客户端
1.安装oracle-instantclient:(默认安装即可) oracle-instantclient12.1-sqlplus-12.1.0.1.0-1.x86_64.rpmoracle-ins ...
- (转)走进JVM,浅水也能捉鱼
这不是一篇描述jvm是什么的文章,也不介绍jvm跨平台的特性,也不是讲述jvm安全特性的文章,更不是讲解jvm指令操作,数据运算的文章,本文重点讲述类型的生命周期. 类型的生命周期涉及到:类的装载.j ...
- 一个Banner广告收缩效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- oracle插入例子
string sql = "insert into EMST_JC_SBXX(XL,SBBM,SBWH,SBMC,CCBM,XNCS,CZXL,ZL,GL,ZZCJ,TCRQ,SYQX,XH ...
- Oracle 获取表结构信息
通过Oracle中的user_tab_cols, user_col_comments, user_constraints, user_cons_columns表联合查询. user_tab_cols用 ...
- eclipse安装egit上传和clone项目到github
一.eclipse安装egit插件 help->new install new software->add location输入http://download.eclipse.org/ ...
- angular 指令梳理 —— checkBox
checkBox 持久化数据为 逗号分割 /** * 功能说明: * htCheckbox 指令用于收集checkbox数据. * 在页面中使用 * 属性指令:ht-checkbox * 对应的值为s ...
- iOS import导入pod第三方库不提示问题
pod 导入第三方库后,使用import 不提示第三方库头文件. 解决办法: 选择target -> BuildSettings -> search Paths 下的 User Heade ...
- C语言中的指针数组和数组指针
代码: #include <iostream> using namespace std; int main(){ ]; ]; cout<<sizeof(a)<<en ...