# 洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)

## 题目描述

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).

## 输入输出格式

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.

```3 70
5 25 50
5 2 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.

```#include<cstring>
#include<cstdio>
#include<cstdlib>
#define min(a,b) (a<b?a:b)
#define max(a,b) (a<b?b:a)
char buf[1<<22],*p1=buf,*p2=buf;
//#define int long long
using namespace std;
const int MAXN=5*1e6+10,INF=1e8+10;
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
int f[MAXN];//恰好为i时的最小花费
int g[MAXN];//完全背包
int val[MAXN],num[MAXN];
int N,T,limit=0;
int main() {
#ifdef WIN32
freopen("a.in","r",stdin);
#endif
memset(f,0x3f,sizeof(f));
memset(g,0x3f,sizeof(g));
g[0]=0;f[0]=0;
for(int i=1;i<=N;i++)
for(int j=val[i];j<=limit;j++)
g[j]=min(g[j],g[j-val[i]]+1);
for(int i=1;i<=N;i++) {
for(int k=1;k<=num[i];k<<=1) {
for(int j=limit;j>=val[i]*k;j--)
f[j]=min(f[j],f[j - val[i]*k]+k);
num[i]-=k;
}
if(num[i])
for(int j=limit;j>=val[i]*num[i];j--)
f[j]=min(f[j],f[j - val[i]*num[i]]+num[i]);
}
int ans=INF;
for(int i=T;i<=limit;i++)
ans=min(ans,f[i]+g[i-T]);
ans==INF?printf("-1"):printf("%d",ans);
return 0;
}```

0 条评论

• ### Kosaraju算法详解

Kosaraju算法可以求出有向图中的强连通分量个数，并且对分属于不同强连通分量的点进行标记。它的算法描述较为简单： (1) 第一次对图G进行DFS遍历，并在...

• ### ZR国庆Round2解题报告

然后刚T3暴力，刚完还有2h左右。。然后，，这时候我zz的选择去打T2的暴力，然而T2暴力真的不是一般的难写。。

• ### 算法提高 金陵十三钗

金陵十三钗 　　本题难度：难 　　本题占分比例：5% 问题描述 　　在电影《金陵十三钗》中有十二个秦淮河的女人要自我牺牲代替十二个女学生去赴日本...

• ### HDUOJ --2544最短路（基础）

输入包括多组数据。每组数据第一行是两个整数N、M（N<=100，M<=10000），N表示成都的大街上有几个路口，标号为1的路口是商店所在地，标号为N的路口是赛...

• ### 古典密码加密解密之多表代换

多表代换密码首先将明文M 分为由n 个字母组成的分组, , … ，对每个分组的加密为 ≡ + ( ), = , , … 其中，（A,B）是密钥，A 是 ...

• ### Golang Leetcode 977. Squares of a Sorted Array.go

更多内容请移步我的repo：https://github.com/anakin/golang-leetcode

• ### HDU 1083 Courses(二分图最大匹配)

题目链接：http://acm.hdu.edu.cn/showproblem.php?pid=1083

• ### LintCode整数排序题目分析解答选择排序插入排序小结

Given an integer array, sort it in ascending order. Use selection sort, bubble s...

• ### 蓝桥杯vip测试题系统试题-算法提高 矩阵转置

给定一个n×m矩阵相乘，求它的转置。其中1≤n≤20，1≤m≤20，矩阵中的每个元素都在整数类型（4字节）的表示范围内。