[USACO06DEC]The Fewest Coins G
题目描述
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).
农夫John想到镇上买些补给。为了高效地完成任务,他想使硬币的转手次数最少。即使他交付的硬 币数与找零得到的的硬币数最少。
John想要买价值为T的东西。有N(1<=n<=100)种货币参与流通,面值分别为V1,V2…Vn (1<=Vi<=120)。John有Ci个面值为Vi的硬币(0<=Ci<=10000)。
我们假设店主有无限多的硬币, 并总按最优方案找零。注意无解输出-1。
输入格式
Line 1: Two space-separated integers: N and T.
Line 2: N space-separated integers, respectively V1, V2, …, VN coins (V1, …VN)
Line 3: N space-separated integers, respectively C1, C2, …, CN
输出格式
Line 1: A line containing a single integer, the minimum number of coins involved in a payment and change-making. If it is impossible for Farmer John to pay and receive exact change, output -1.
样例 #1
样例输入 #1
3 70
5 25 50
5 2 1
样例输出 #1
3
提示
Farmer John pays 75 cents using a 50 cents and a 25 cents coin, and receives a 5 cents coin in change, for a total of 3 coins used in the transaction.
明显购买是一个多重背包(二进制优化及以上),找零是一个完全背包,唯一的问题是确定购买的最大 M M M 。
题解中说 M = m a x ( v [ i ] 2 ) M = max(v[i]^2) M=max(v[i]2) 我不会证明,也没看懂,不过, M = ∑ v [ i ] M = \sum v[i] M=∑v[i] 是明显成立的,然后跑一个枚举就行。
/*
A: 10min
B: 20min
C: 30min
D: 40min
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <assert.h>
#include <sstream>

本文介绍了一个关于硬币交易优化的问题,农夫John希望在购买物品时使得硬币交换次数最少。他面临多种面值的硬币和有限的库存。问题涉及到多重背包和完全背包的组合优化,通过建立数学模型并使用动态规划求解。文章提供了样例代码,展示如何找到最小的硬币总数,包括支付和找零。
最低0.47元/天 解锁文章
465

被折叠的 条评论
为什么被折叠?



