题意

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. STL专题·vector容器

    1.构造函数 vector():创建一个空vector vector(int nSize):创建一个vector,元素个数为nSize (vector<int> a(10);) vecto ...

  2. acm--1006

    Problem Description The three hands of the clock are rotating every second and meeting each other ma ...

  3. VUE通过索引值获取数据不渲染的问题

    问题:vue里面当通过索引值获取数据时,ajax数据成功返回,但是在火狐下不渲染 解决:

  4. Hive(5)-DDL数据定义

    一. 创建数据库 CREATE DATABASE [IF NOT EXISTS] database_name [COMMENT database_comment] [LOCATION hdfs_pat ...

  5. ElasticSearch 集群安装,简单使用

    http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html https://gith ...

  6. Flink+Kafka整合的实例

    Flink+Kafka整合实例 1.使用工具Intellig IDEA新建一个maven项目,为项目命名为kafka01. 2.我的pom.xml文件配置如下. <?xml version=&q ...

  7. SpaceVim 语言模块 elixir

    原文连接: https://spacevim.org/cn/layers/lang/elixir/ 模块简介 功能特性 启用模块 快捷键 语言专属快捷键 交互式编程 运行当前脚本 模块简介 这一模块为 ...

  8. CentOS 搭建 Git 服务器

    官方文档移步 Git 服务器的搭建 安装 Git #yum install git 创建 Git 专用用户 #useradd git,改密码 #passwd git,切换至 Git 用户 #su gi ...

  9. Canvas在移动端设备上模糊出现锯齿边

    在绘制的过程中画布内容的实际大小是根据 canvas 的 width 与 height 属性设置的,而 style 或者CSS设置的width 与 height 只是简单的对画布进行缩放. canva ...

  10. 怎样才能使用ChipScope 加入被优化掉的信号

    在调试过程中常常遇到的一个问题就是,xilinx工具在逻辑综合的过程中,将自己RTL代码中的很多变量都优化掉了,使得调试的抓信号的过程很纠结.以下是解决方法: 1.右键synthesis,在综合选项里 ...