【POJ3260】【The Fewest Coins】
来源:互联网 发布:三网通彩信软件 编辑:程序博客网 时间:2024/05/16 16:18
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 C1coins 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 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
Source
//f1[i]表示支付i元所需的最小硬币数//f2[i]表示找i元所需的最小硬币数//买T元东西所需的最小硬币数为f1[T + i] - f2[i]//f1[i] = min(f1[i], f1[i - k] + 1)//f2[i] = min(f2[i], f2[i - k] + 1)//其中f1[i]多重背包,用二进制拆分求解//f2[i]为完全背包//其中:给钱上界为:T+maxValue^2,其中maxValue为最大硬币面值。//证明:反证法。假设存在一种支付方案,John给的钱超过T+maxValue^2, //则售货员找零超过maxValue^2,则找的硬币数目超过maxValue个,将其看作一数列,//求前n项和sum(n),根据鸽巢原理,至少有两 个对maxValue求模的值相等,//假设为sum(i)和sum(j),i<j,则i+1...j的硬币面值和为maxValue的倍数,//同理,John给的钱中也有 一定数量的硬币面值和为maxValue的倍数,//则这两堆硬币可用数量更少的maxValue面值硬币代替,产生更优方案。#include <iostream>#include <cmath>#include <cstring>using namespace std;const int MAXN = 101;const int MAXT = 10000 + 120 * 120 + 1;const int INF = 200000000;int f1[MAXT];int f2[MAXT];int v[MAXN];int c[MAXN];int main() {//freopen("1.txt", "r", stdin);int n, T;while(cin >> n >> T) {int maxV = 0;for(int i = 0; i < n; i++) {cin >> v[i];maxV = max(maxV, v[i]);}maxV *= maxV;int maxT = T + maxV;for(int i = 0; i < n; i++)cin >> c[i];for(int i = 0; i <= maxT; i++)f1[i] = f2[i] = INF;f1[0] = f2[0] = 0;//完全背包求解for(int i = 0; i < n; i++) {for(int j = v[i]; j < maxV; j++) {f2[j] = min(f2[j], f2[j - v[i]] + 1);}}//多重背包求解for(int i = 0; i < n; i++) {int k = 1;int sum = 0;while(sum < c[i]) {for(int j = maxT; j >= v[i] * k; j--) {f1[j] = min(f1[j], f1[j - v[i] * k] + k);}sum += k;if(sum + k * 2 > c[i])k = c[i] - sum;elsek *= 2;}}int _min = INF;for(int i = T; i <= maxT; i++) {_min = min(_min, f1[i] + f2[i - T]);}if(_min == INF)printf("-1/n");elseprintf("%d/n", _min);} return 0;}
- poj3260 The Fewest Coins
- poj3260 The Fewest Coins
- 【POJ3260】【The Fewest Coins】
- 【POJ3260】【The Fewest Coins】
- POJ3260 The Fewest Coins
- POJ3260:The Fewest Coins(混合背包)
- POJ3260---The Fewest Coins(混合背包)
- POJ3260--The Fewest Coins(混合背包)
- POJ3260 The Fewest Coins 多重背包+完全背包
- 【POJ3260】The Fewest Coins 多重背包+完全背包
- poj3260[USACO2006,Dec]The Fewest Coins最少找零
- POJ The Fewest Coins
- The Fewest Coins POJ
- 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 3579 二分+lower_bound
- angularJS---自定义过滤器
- 13.键-值编码(KVC)
- 指针函数与函数指针的区别
- Java之反射机制【入门版(1)】
- 【POJ3260】【The Fewest Coins】
- 黑马程序员——泛型
- Implement Stack using Queues
- 机器学习(吴恩达授课)课堂笔记(1)- Intro
- Java 静态代码块 初始化块 构造方法的执行顺序
- android 中 View的构造函数
- n!素因子分解
- 59 - 对称的二叉树
- shell流程控制语句