题目描述

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(1<=T<=10000)样东西(2017-7-20 管理员注:这个翻译有问题,实际为要买价值为T的东西)。有N(1<=n<=100)种货币参与流通,面值分别为V1,V2..Vn (1<=Vi<=120)。John有Ci个面值为Vi的硬币(0<=Ci<=10000)。我们假设店主有无限多的硬币, 并总按最优方案找零。

输入输出格式

输入格式:

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: 复制

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.

思路比较简单

对john做一次多重背包

对店主做一次完全背包(然而不会写代码)

多重背包用二进制优化

另外,本蒟蒻不怎么懂为什么枚举上界是所有面值相乘再加T,

刚开始写面值乘数量死活RE QWQ...

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

最新文章

  1. mysql5.7碰到的坑
  2. Ionic2 rc2 Events 跨界面调用的使用方法及问题解决
  3. Delphi公司的生存哲学
  4. 【原创】Eclipse中为SVN设置快捷键
  5. CSS常见的浏览器前缀
  6. QT 读写sqllite数据库
  7. 让qq图标在自己的网站上显示方法
  8. 安装程序时出现错误代码0x80070422
  9. Spring 3 AOP 概念及完整示例
  10. python 使用标准库根据进程名获取进程的pid
  11. java字符串应用之字符串编码转换
  12. 2019-04(2)Python学习
  13. elasticsearch 通过HTTP RESTful API 操作数据
  14. 初窥Java之二
  15. JDBC执行SQL语句以及Date对象和字符串之间的相互转换(关键是那张标准表)
  16. 下载fiddler证书并设置信任
  17. Eclipse调试DEBUG时快速查看某个变量的值的快捷键、快速跳转到某行的快捷键
  18. zabbix items 配置
  19. CentOS 6.5配置SSH免密码登录
  20. 五子棋AI算法

热门文章

  1. Model2
  2. 为什么使用dispatch_sync
  3. 判断input或者div.span等标签是否存在
  4. Codeforces Round #547 (Div. 3) E. Superhero Battle
  5. 【Educational Codeforces Round 53 (Rated for Div. 2) C】Vasya and Robot
  6. 【codeforces 483B】Friends and Presents
  7. 教你 Shiro + SpringBoot 整合 JWT
  8. 转载 - 算法实践——舞蹈链(Dancing Links)算法求解数独
  9. 0809MySQL实战系列:大字段如何优化|数据存储结构
  10. 使用厂商MIB库查找设备OID值并实施监控的方法