POJ 3260 The Fewest Coins(多重背包+完全背包)
来源:互联网 发布:邹倚天 知乎 编辑:程序博客网 时间:2024/06/10 08:47
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 hasN (1 ≤ N ≤ 100) different coins, with values V1,V2, ..., VN (1 ≤ Vi ≤ 120). Farmer John is carryingC1 coins of value V1, C2 coins of valueV2, ...., 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 V1, V2, ...,VN coins ( V1, ... VN)
Line 3: N space-separated integers, respectively C1, C2, ...,CN
Output
Sample Input
3 705 25 505 2 1
Sample Output
3
Hint
解题报告:
题意和思路就直接贴EaShion1994博客里的了:http://blog.csdn.net/EaShion1994/article/details/43744767
题意:定义一共有N种货币,对应V1….Vn,N种价格。老农要拿一些钱去买价格为T的东西,老农有C1…..CN个对应货币,售货员有无限个货币。求如何才能使老农拿的硬币数与找零的硬币数和最小。
算法:
老农上界:maxv*maxv+T;售货员上界:maxv*maxv
证明(参考网上):如果John的付款数大于了maxv*maxv+T,即付硬币的数目大于了maxv,根据鸽笼原理,至少有两个的和对maxv取模的值相等,也就是说,这部分硬币能够用更少的maxv来代替。证毕。
售货员:完全背包
。至于上界为什么要这样确定,还不是很懂。但是如果不确定上界,就开得大一些吧。
#include <cstdio>#include <cstring>#define min(a, b) (a < b ? a : b)#define MAXV 121#define MAXT 10000//dp[i]表示dp凑够i元所用的最小货币数。int farmer[MAXV * MAXV + MAXT], shopkeeper[MAXV * MAXV];int n, t, v[MAXV], num[MAXV], i, ans, maxv;void ZeroOnePack(int *dp, int cost, int V, int cnt) { for (int j = V; j >= cost; j --) { dp[j] = min(dp[j], dp[j-cost] + cnt); }}void CompletePack(int *dp, int cost, int V) { for (int j = cost; j <= V; j ++) { dp[j] = min(dp[j], dp[j-cost] + 1); }}void MultiPack(int *dp, int cost, int n, int V) { if(cost * n >= V - cost + 1) { CompletePack(dp, cost, V); } else { int k = 1; while (k < n) { ZeroOnePack(dp, k * cost, V, k); n -= k; k *= 2; } ZeroOnePack(dp, n * cost, V, n); }}int main() { while (scanf("%d%d", &n, &t) != EOF) { //init ans = 0x7ffffff, maxv = 0; memset(farmer, 0x3f, sizeof(farmer)), farmer[0] = 0; memset(shopkeeper, 0x3f, sizeof(shopkeeper)), shopkeeper[0] = 0; for (i = 0; i < n; i ++) { scanf("%d", v + i); if(maxv < v[i]) maxv = v[i]; } maxv *= maxv; for (i = 0; i < n; i ++) { scanf("%d", num + i); } //solve for (i = 0; i < n; i ++) { MultiPack(farmer, v[i], num[i], maxv + t); CompletePack(shopkeeper, v[i], maxv); } for (i = 0; i <= maxv; i ++) { if(ans > farmer[i + t] + shopkeeper[i]) { ans = farmer[i + t] + shopkeeper[i]; } } //output if(ans == 0x7ffffff) { printf("-1\n"); } else { printf("%d\n", ans); } } return 0;}
当然这道题的价值和代价是相等的,这让我们又想起多重背包特例的O(NV)算法,用一个数组来统计货币的使用次数。代码就贴上来供大家参考:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <set>#include <queue>using namespace std;#define maxn 15005#define inf 0x7fffffpair <int ,int > p;int dp1[maxn];int dp2[maxn];int v[105];int used[maxn];int c[maxn];int main(){int k,t,n;while(scanf("%d%d",&n,&t)!=EOF){for(int i=1;i<=n;i++){scanf("%d",&v[i]);}for(int j=1;j<=n;j++){ scanf("%d",&c[j]);}for(int i=1;i<maxn;i++){dp1[i]=dp2[i]=inf;}dp1[0]=dp2[0]=0;for(int i=1;i<=n;i++){memset(used,0,sizeof(used));for(int j=v[i];j<maxn;j++){if(dp1[j-v[i]]+1<dp1[j]&&used[j-v[i]]<c[i]){dp1[j]=dp1[j-v[i]]+1;used[j]=used[j-v[i]]+1;}}}for(int i = 1;i <= n;i++){for(int j = v[i];j < maxn;j++){dp2[j]=min(dp2[j],dp2[j-v[i]]+1);}}int ans = inf;for(int i = t; i < maxn;i++){ans = min(ans,dp1[i]+dp2[i-t]);}if(ans >= inf)ans=-1;printf("%d\n",ans);}return 0;}
- poj 3260 The Fewest Coins(多重背包+完全背包)
- poj 3260 The Fewest Coins (多重背包 + 完全背包)
- POJ 3260 The Fewest Coins(多重背包+完全背包)
- POJ 3260 The Fewest Coins(多重背包+完全背包)
- poj 3260 The Fewest Coins(多重背包+完全背包)
- POJ 3260 The Fewest Coins 多重背包+完全背包
- POJ 3260 The Fewest Coins (多重背包 + 完全背包)
- POJ 3260 - The Fewest Coins(完全背包+多重背包)
- POJ 3260 The Fewest Coins(多重背包+完全背包)
- POJ 3260 The Fewest Coins(完全背包+多重背包(转化为01背包))
- POJ 3260 The Fewest Coins(多重背包+完全背包—>交易数量最小)@
- POJ 3260 The Fewest Coins(动态规划+多重背包+完全背包)
- POJ - 3260 The Fewest Coins (完全背包)
- POJ 3260 The Fewest Coins (混合背包--多重背包+完全背包)
- poj 3260 The Fewest Coins(背包)
- poj 3260 The Fewest Coins(背包)
- POJ3260 The Fewest Coins 多重背包+完全背包
- 【POJ3260】The Fewest Coins 多重背包+完全背包
- 小99乘法 for循环
- [资源] 好东西,留着哈哈
- 算法训练 结点选择 【树形dp】
- ui.TextView及其子类
- ASP.Net总结
- POJ 3260 The Fewest Coins(多重背包+完全背包)
- MySQL的卸载
- Java 枚举式转码
- (\\d{17}[0-9a-zA-Z]|\\d{14}[0-9a-zA-Z]) ②\\d{6}(\\d{8}).* 这两个正则表达式各自代表什么意思
- C++ 重载调用操作符和函数对象
- POJ 1003
- 网络性能测试iperf
- POJ 1004
- netduino plus 2使用interop出现MMP : error MMP0000: CLR_E_FAIL错误解决方式