POJ 3260 The Fewest Coins 最少硬币个数(完全背包+多重背包,混合型)
题意:FJ身上有各种硬币,但是要买m元的东西,想用最少的硬币个数去买,且找回的硬币数量也是最少(老板会按照最少的量自动找钱),即掏出的硬币和收到的硬币个数最少。
思路:老板会自动找钱,且按最少的找,硬币数量也不限,那么可以用完全背包得出组成每个数目的硬币最少数量。而FJ带的钱是有限的,那么必须用多重背包,因为掏出的钱必须大于m,那么我们所要的是大于等于m钱的硬币个数,但是FJ带的钱可能很多,超过m的很多倍都可能,那么肯定要有个背包容量上限,网上说的根据抽屉原理是m+max*max,这里的max指的是最大面值。而给多了的钱上限是max*max,那么找回的钱也必须是max*max,所以完全背包部分的背包容量是max*max。穷举这max*max个可能就行了。
我的思路:与上面不同的是多重背包的容量应该是m+max,因为如果需要找回的钱大于max,那么老板也只是拿多几张最大面额的给你而已。比如买条烟1329块钱,13+1+1+4=19张RMB, 那么我们可以给他14张,15张,16张,17张,18张100的,老板会相应找回71块,171块,271块,371块,471块,你再往上加钱的话,老板也只是拿更多的100还你,这是多余的。那么最多不会超过一张一百(最大面额)的,也就是1329+100=1429为背包容量。错了很多次!
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#define INF 0x0ffffffa
using namespace std;
const int N=;
const int limit=;
int n, t;
int com[limit]; //完全背包
int mul[limit]; //多重背包
int big;
struct node
{
int v,c;
}coin[N]; int cal()
{
for(int i=; i<=big*big; i++) com[i]=INF;
com[]=;
for(int i=; i<n; i++) //完全背包
{
for(int j=; j+coin[i].v<=big*big; j++) //上限big*big
{
if(com[j+coin[i].v]>com[j]+)
com[j+coin[i].v]= com[j]+ ;
} } int upto= t+ big*big; //多重背包上限
for(int i=; i<=upto; i++) mul[i]=INF;
mul[]=;
for(int i=; i<n; i++ ) //多重背包:01背包+二进制
{
int k=,tmp=coin[i].c;
while()
{
if(k>tmp&&tmp) k=tmp;
else if(k>tmp) break;
for(int j=upto; j>=k*coin[i].v; j-- )
if(mul[j-k*coin[i].v ]!=INF&&mul[j-k*coin[i].v]+k<mul[j])
mul[j]=mul[j-k*coin[i].v]+k;
tmp-=k;
k<<=;
}
}
int ans=mul[t]; //刚好给t元
for(int i=t+,j=; i<upto; i++,j++ )
{
if(com[j]==INF || mul[i]==INF) continue; //INF的表示不能刚好凑成这个价,滤掉。
else if(ans>mul[i]+com[j] ) ans= mul[i]+com[j];
} if(ans==INF) return -;
return ans;
}
int main()
{
//freopen("input.txt","r",stdin);
while(cin>>n>>t)
{
big=-;
for(int i=; i<n; i++)
{
scanf("%d",&coin[i].v);
if(big<coin[i].v) big=coin[i].v;
}
for(int i=; i<n; i++) scanf("%d",&coin[i].c);
printf("%d\n",cal());
} return ;
}
AC代码
POJ 3260 The Fewest Coins 最少硬币个数(完全背包+多重背包,混合型)的更多相关文章
- 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: ...
- POJ 3260 The Fewest Coins(背包问题)
[题目链接] http://poj.org/problem?id=3260 [题目大意] 给出你拥有的货币种类和每种的数量,商店拥有的货币数量是无限的, 问你买一个价值为m的物品,最少的货币流通数量为 ...
- POJ 3260 The Fewest Coins(多重背包问题, 找零问题, 二次DP)
Q: 既是多重背包, 还是找零问题, 怎么处理? A: 题意理解有误, 店主支付的硬币没有限制, 不占额度, 所以此题不比 1252 难多少 Description Farmer John has g ...
- poj 3260 The Fewest Coins
// 转载自http://blog.163.com/benz_/blog/static/18684203020115721917109/算法不难看出,就是一个无限背包+多重背包.问题在于背包的范围.设 ...
- (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)
http://poj.org/problem?id=3260 Description Farmer John has gone to town to buy some farm supplies. ...
- 洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)
题目描述 Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always p ...
- POJ 3260 完全背包+多重背包+思维
传送门:https://vjudge.net/problem/20465/origin 题意:你有n种钞票,面值为c[i],数量为v[i],便利店老板有无数张面值为c[i]的钞票,问你买一个价值为T的 ...
- poj 1742 Coins(二进制拆分+bitset优化多重背包)
\(Coins\) \(solution:\) 这道题很短,开门见山,很明显的告诉了读者这是一道多重背包.但是这道题的数据范围很不友好,它不允许我们直接将这一题当做01背包去做.于是我们得想一想优化. ...
随机推荐
- POI 中的CellType类型以及值的对应关系
操作使用POI接口,了解CellType的类型和值的对应关系. CellType 类型 值 CELL_TYPE_NUMERIC 数值型 0 CELL_TYPE_STRING 字符串型 1 CELL_T ...
- AngularJs(Part 4)--Modules depending on other Modules
Angular does an excellent job of managing object dependencies. it can even take care of module depen ...
- poj3535 A+B (大数加法)
A+B Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 811 Accepted: 371 Description The ...
- jquery中的$.ajax()的源码分析
针对获取到location.href的兼容代码: try { ajaxLocation = location.href; } catch( e ) { // Use the href attribut ...
- vb常用命名空间
摘自:http://www.2cto.com/kf/201211/170837.html 感谢 (一)如下是系统中分离出来Imports MicrosoftImports Microsoft.CSha ...
- LeetCode: 371 Sum of Two Integers(easy)
题目: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. ...
- 2014 Noip提高组 Day2
P2038 无线网络发射器选址 [题目描述] 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129 条东西向街道和129 ...
- [Xcode 实际操作]六、媒体与动画-(16)实现音乐的背景播放
目录:[Swift]Xcode实际操作 本文将演示音乐的背景播放功能 打开项目信息配置文件[info.plist]. 需要在配置文件中进行一些操作,使程序支持音乐的背景播放. 点击鼠标右键,弹出右键菜 ...
- Solidity 最新 0.5.8 中文文档发布
本文首发于深入浅出区块链社区 热烈祝贺 Solidity 最新 0.5.8 中文文档发布, 这不单是一份 Solidity 速查手册,更是一份深入以太坊智能合约开发宝典. 翻译说明 Solidity ...
- IBM WebSphere MQ
相关链接: http://kakajw.iteye.com/category/269774 http://www.ibm.com/support/knowledgecenter/zh/SSFKSJ_7 ...