题意

n个硬币,q次询问。第二行给你n个硬币的面值(保证都是2的次幂!)。每次询问组成b块钱,最少需要多少个硬币?

Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2 解题思路:总体上使用的是贪心策略,从最大面值的往下贪心选择就可以了,由于数据量较大这里使用了map,这样就最多才32个数。第一次使用map的迭代器

反向迭代器的rbegin和rend的位置
和正向迭代器的begin和end的位置如下图


 
 #include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
map<int,int>mp;///这里键存储的是硬币的面值,值存储的是硬币的个数
int main()
{
int n,m,i;
scanf("%d%d",&n,&m);
for(i=0; i<n; i++)
{
int x;
scanf("%d",&x);
mp[x]++;
}
while(m--)
{
int ans=0;
int flag=0;
int a,t;
scanf("%d",&a);
map<int,int>::reverse_iterator it;///反向迭代器
for(it=mp.rbegin(); it!=mp.rend(); it++)
{
t=min(a/it->first,it->second);
ans+=t;///t只有是整数的时候才会用来计数
a=a-t*it->first;
if(!a)///当a=0是恰好完全取完
{
flag=1;
break;
}
}
if(flag==1)
{
printf("%d\n",ans);
}
else
{
printf("-1\n");
}
}
return 0;
}

Coins and Queries(map迭代器+贪心)的更多相关文章

  1. CF1003D Coins and Queries 贪心

    Coins and Queries time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  2. CF 1003D Coins and Queries【位运算/硬币值都为2的幂/贪心】

    Polycarp has n coins, the value of the i-th coin is ai. It is guaranteed that all the values are int ...

  3. Codeforces Round #494 (Div. 3) D. Coins and Queries (贪心,数学)

    题意:给你一组全是\(2^d\ (d\ge0)\)的数,询问q次,每次询问一个数,问这个数是否能够由原数组中的数相加得到,如果能,输出最少用多少个数,否则输出\(-1\). 题解:首先贪心得出结论:如 ...

  4. Codeforces Round #494 (Div. 3) D. Coins and Queries(贪心

    题目链接 题目大意:给你n个物品,第iii个物品价值aia_iai​,询问q次,问你能不能凑出价值为qiq_iqi​的物品. 小贪心吧.从大到小找,能拿就拿就行了. #include<bits/ ...

  5. Coins and Queries(codeforce 1003D)

    Polycarp has nn coins, the value of the i-th coin is aiai . It is guaranteed that all the values are ...

  6. Codeforces Round #371 (Div. 2) C. Sonya and Queries[Map|二进制]

    C. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. 给jdk写注释系列之jdk1.6容器(6)-HashSet源码解析&Map迭代器

    今天的主角是HashSet,Set是什么东东,当然也是一种java容器了.      现在再看到Hash心底里有没有会心一笑呢,这里不再赘述hash的概念原理等一大堆东西了(不懂得需要先回去看下Has ...

  8. 关于set和map迭代器支持的运算

    问题: 曾经想遍历一个set遍历.当时是这样写的: set<int>::iterator b = a.begin()+1 后来发现程序报错.究其原因是,set迭代器不支持加减数操作. 查看 ...

  9. Planning The Expedition(暴力枚举+map迭代器)

    Description Natasha is planning an expedition to Mars for nn people. One of the important tasks is t ...

随机推荐

  1. RPM包、YUM、system初始化进程基本知识

  2. redhat系统升级openssh到7.5

    注意,注意,注意重要的事情说三遍,关于ssh的升级不能完全按照别人的教程进行升级,因为每台生产机器都是不一样的,有可能别人能升级成功但是另外一个就可能会失败,因为每台机器上面跑的应用是不一样的,涉及到 ...

  3. Redis(八):Redis的复制(Master/Slave)

    Redis的复制(Master/Slave)目录导航: 是什么 能干嘛 怎么玩 复制原理 哨兵模式(sentinel) 复制的缺点 是什么 官网 行话:也就是我们所说的主从复制,主机数据更新后根据配置 ...

  4. 一分钟完成pip安装

    很多实用Python的小伙伴都需要使用pip安装相应的包,对于初学者而已,检查遇到pip安装不成功的情况,如以下典型错误: Traceback (most recent call last): Fil ...

  5. 将图片绘制到画布上:imagecopy()

    <?php //1. 绘制图像资源(创建一个画布) $image = imagecreatetruecolor(500, 300); //2. 先分配一个绿色 $green = imagecol ...

  6. 20190121-n个人围成一圈,凡报到3的人退出圈子,最后留下的是原来第几号的那位

    1. 报数问题:有n个人围成一圈,顺序排号.从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位 思路:此题主要问题在于但凡报到3的人退出圈子,而报数的号码与圈子的 ...

  7. django创建第一个子应用-3

    在Web应用中,通常有一些业务功能模块是在不同的项目中都可以复用的,故在开发中通常将工程项目拆分为不同的子功能模块,各功能模块间可以保持相对的独立,在其他工程项目中需要用到某个特定功能模块时,可以将该 ...

  8. Django学习之模拟架构页面跳转

    背景知识,需要有一定量的HTTP基础知识 在客户端游览器通过URL向服务端发送请求时,经历了两次过程.一次是URL向服务端发起请求,一次是服务端向客户端回发响应. 由图可知,客户端一共传递两个信息,一 ...

  9. 创建IDataProvider实例

    using System; namespace Demo.Data{ public class DatabaseProvider { private static IDataProvider _ins ...

  10. ChipScope Pro Inserter - "ERROR:NgdBuild:924 - bidirect pad net '<oDRAM0_A>' is driving non-buffer primitives

    解决方案: Using a IOBUF signal as a trigger for the ILA Inserter flow will cause a NGDBuild error. These ...