CodeForces 524 C.The Art of Dealing with ATM(枚举)
来源:互联网 发布:手机airdrop找不到mac 编辑:程序博客网 时间:2024/06/05 01:00
Description
一家银行的ATM机中有n种纸币,每种纸币面额为a[i],数量无限,但每次取款只能取最多k张最多两种面额的纸币,有q次查询,每次查询是否可以取x元,如果可以那最少取多少张纸币可以取到x元
Input
第一行两个整数n,k分别表示纸币种类数和每次取款数量上限,之后n个整数a[i]表示这n种纸币的面额,然后输入一整数q表示查询数,每次查询输入一整数x(1<=n<=5000,1<=k<=20,1<=a[i]<=1e7,1<=q<=20,1<=x<=2e8)
Output
如果不能取到x则输出-1,否则输出要取的最少纸币数量
Sample Input
6 20
10 50 100 500 1000 5000
8
4200
100000
95000
96000
99000
10100
2015
9950
Sample Output
6
20
19
20
-1
3
-1
-1
Solution
把纸币的面额放到map里,枚举第一种纸币的种类和数量,如果恰好是x则更新答案,否则枚举第二种纸币的数量,那么第二种纸币的面额就确定了,从map找这种面额是否存在如果存在则更新答案
Code
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<queue>#include<map>#include<set>#include<ctime>using namespace std;typedef long long ll;#define INF 0x3f3f3f3f#define maxn 5555map<ll,int>m;int n,k,a[maxn],q;int main(){ while(~scanf("%d%d",&n,&k)) { m.clear(); for(int i=1;i<=n;i++)scanf("%d",&a[i]),m[a[i]]=1; scanf("%d",&q); while(q--) { int ans=-1; ll x; scanf("%I64d",&x); for(int i=1;i<=n;i++) for(int j=1;j<=k&&1ll*a[i]*j<=x;j++) { ll t=x-1ll*a[i]*j; if(t==0) { if(ans==-1)ans=j; else ans=min(ans,j); } else { for(int l=1;l+j<=k;l++) if(t%l==0&&m.find(t/l)!=m.end()) { if(ans==-1)ans=j+l; else ans=min(ans,j+l); } } } printf("%d\n",ans); } } return 0;}
阅读全文
0 0
- CodeForces 524 C.The Art of Dealing with ATM(枚举)
- Codeforces 529E The Art of Dealing with ATM 简单题
- CF524C:The Art of Dealing with ATM(打表 + 二分)
- VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E---The Art of Dealing with ATM
- The Art of Debugging with GDB
- Codeforces 451C Predict Outcome of the Game (枚举+YY)
- Dealing with WMI Timeouts (c#)
- 强参数 Strong parameters: Dealing with mass assignment in the controller instead of the model
- The Lost Art of C Structure Packing
- The Lost Art of C Structure Packing
- 汇编(The art of assembly language)
- The Art of Java
- The Art of C++
- The Art of SQL
- The Art of Rails
- the art of conclusion
- The art of deception
- the Art of Life
- source insight utf8设置
- 检查数组是否包含某个值的方法
- BI怎么选?重点看这10个技术指标
- 设计模式简介
- 边框扩张
- CodeForces 524 C.The Art of Dealing with ATM(枚举)
- PAT 1066图像过滤 C
- Key Task HDU
- 如何让一个很长的单词在div里自动换行
- iOS UILabel根据字数自适应宽高以及控件按屏幕大小按比例缩放
- memcache和redis的区别
- Java_基础—Throwable的几个常见方法
- 网络攻击技术开篇——SQL Injection
- “GANs 之父”Goodfellow亲身传授:深度学习未来的8大方向和入门AI必备的三大技能