cf524C The Art of Dealing with ATM
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.
For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.
Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20).
The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order.
The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make.
The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print - 1, if it is impossible to get the corresponding sum.
6 20
10 50 100 500 1000 5000
8
4200
100000
95000
96000
99000
10100
2015
9950
6
20
19
20
-1
3
-1
-1
5 2
1 2 3 5 8
8
1
3
5
7
9
11
13
15
1
1
1
2
2
2
2
-1
因为只能取2种面值,所以枚举一种面值,然后搞个map找另一个面值跟它匹配
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,k,q,cnt;
int a[];
struct p{int v,w,k;}b[];
bool operator <(p a,p b){return a.w<b.w||a.w==b.w&&a.k<b.k;}
map<int,pa>mp,mp2;
int main()
{
mp.clear();mp2.clear();
n=read();k=read();
for (int i=;i<=n;i++)
{
a[i]=read();
for (int j=;j<=k;j++)
b[++cnt].v=i,b[cnt].k=j,b[cnt].w=j*a[i];
}
sort(b+,b+cnt+);
for (int i=;i<=cnt;i++)
{
int w=b[i].w,kk=b[i].k,v=b[i].v;
if (mp[w]==mkp(,))mp[w]=mkp(kk,v);
else
{
if (mp[w].first>kk)mp2[w]=mp[w],mp[w]=mkp(kk,v);
else if (mp2[w].first>kk)mp2[w]=mkp(kk,v);
}
}
q=read();
for (int i=;i<=q;i++)
{
int x=read(),ans=;
for (int j=;j<=n;j++)
{
for (int l=;l<=k;l++)
{
int now=l*a[j];
if (now>x)break;
if (now==x){ans=min(ans,l);break;}
pa s=mp[x-now];
if (s.second==j)s=mp2[x-now];
if (s==mkp(,))continue;
ans=min(ans,s.first+l);
}
}
printf("%d\n",ans>k?-:ans);
}
}
cf524C
cf524C The Art of Dealing with ATM的更多相关文章
- Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!
VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM Time Lim ...
- codeforce The Art of Dealing with ATM
题目大意 ATM取款机有n种不同的钱币kind[i],每次取款允许吐出不超过k张钱币,且钱币的种类数不能超过2(一开始没理解2的意思),现在有q次取款,钱数为ques,问ATM能否凑出这样的钱,若能的 ...
- CodeForces 524C The Art of Dealing with ATM (二分)
题意:给定 n 种不同的钞票,然后用q个询问,问你用最多k张,最多两种不同的钞票能不能组成一个值. 析:首先如果要求的值小点,就可以用DP,但是太大了,所以我们考虑一共最多有n * k种钞票,如果每次 ...
- Codeforces 524C.The Art of Dealing with ATM(暴力)
我先采用了智障解法(n * n枚举...刚开始把n看成1000了还以为能过) 理所当然的t了,不过我怀疑优化一下能过?(感觉数据不太行的亚子 然后就是O(n * k * k)的解法,看到好多人快乐二分 ...
- [转] Loren on the Art of MATLAB
http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/ Loren ...
- 1179: [Apio2009]Atm
1179: [Apio2009]Atm Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 1629 Solved: 615[Submit][Status ...
- The Art of Deception
前言 一些黑客毁坏别人的文件甚至整个硬盘,他们被称为电脑狂人(crackers)或计算机破坏者(vandals).另一些新手省去学习技术的麻烦,直接下载黑客工具侵入别人的计算机,这些人被称为脚本小子( ...
- 设计模式(十二):通过ATM取款机来认识“状态模式”(State Pattern)
说到状态模式,如果你看过之前发布的重构系列的文章中的<代码重构(六):代码重构完整案例>这篇博客的话,那么你应该对“状态模式”并不陌生,因为我们之前使用到了状态模式进行重构.上一篇博客我们 ...
- How the problem solved about " Dealing with non-fast-forward errors"
Recently ,I got confused When I use git to push one of my project. The problem is below: And I Foun ...
随机推荐
- Vmware 安装CentOS7时连不上网问题的解决
在VmWare 上安装Centos7时,装好vmware后还是连不上网,通过查找资料原来是因为有线网卡没有激活,默认centos和redhat7都是不启用有线网卡的,要么手动开启,要么安装时直接启用! ...
- Ubuntu16.04+GTX1080配置TensorFlow并实现图像风格转换
1. TensorFlow TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,表达了高层次的机器学习计算,大幅简化了第一代系统,并且具备更好的灵活性和可延展性. Te ...
- Template 基础篇-函数模板(待看
Template 基础篇-函数模板 Template所代表的泛型编程是C++语言中的重要的组成部分,我将通过几篇blog对这半年以来的学习做一个系统的总结,本文是基础篇的第一部分. Template ...
- NOIP2016——一个逗号引发的血案
今年江西省报名人数一下子增起来了 隔壁中学来了80+人(虽然都是来给我们垫底的...临时被老师抓来上战场 总之我们赛区参赛人数总算多起来了(起码没再减50%...连续4年减50%真不是随便说说的... ...
- ubuntu安装easygui模块
使用pip安装easygui 如果未安装pip,则使用如下命令 sudo apt-get install python-pip 安装完pip后,使用如下命令安装easygui sudo pip ins ...
- 命令行下创建MySQL数据库与创建用户以及授权
先以root用户登录mysql: C:\Users\XXX>mysql -u root -p 输入密码后登录,接下来操作如下: 1.创建数据库 语法:create schema [数据库名称] ...
- MySQL迁移升级解决方案
任务背景 由于现有业务架构已不能满足当前业务需求,在保证数据完整的前提下,现需要将原有数据库迁移到另外一台单独的服务器上,在保证原有服务正常的情况下,将原有LAMP环境中mysql数据库版本5.6.3 ...
- Hadoop4.2HDFS测试报告之三
第一组:文件存储写过程记录 NameNode:1 DataNode:1 本地存储 scp localpath romotepath 500 2 1 23.67 NameNode:1 DataNode: ...
- 2049: [Sdoi2008]Cave 洞穴勘测(LCT)
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 9962 Solved: 4824[Submit] ...
- HDU 3594 Cactus 有向仙人掌图判定
题意 给出一个有向图,并给出仙人掌图的定义 图本身是强连通的 每条边属于且只属于一个环 判断输入的图是否是强连通的. 分析 杭电OJ上的数据比较弱,网上一些有明显错误的代码也能AC. 本着求真务实的精 ...