题意

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. Linux 只显示目录或者文件方法

    ls 参数 -a 表示显示所有文件,包含隐藏文件-d 表示显示目录自身的属性,而不是目录中的内容-F 选项会在显示目录条目时,在目录后加一个/ 只显示目录 方法一: find . -type d -m ...

  2. iOS 12 真机调试 Xcode 9 提示 Could not locate device support files.

    升级 iOS 12 之后,使用 Xcode 9 真机调试会提示错误: Could not locate device support files. This iPhone 6 Plus is runn ...

  3. Apache Spark on K8s的安全性和性能优化

    前言 Apache Spark是目前最为流行的大数据计算框架,与Hadoop相比,它是替换MapReduce组件的不二选择,越来越多的企业正在从传统的MapReduce作业调度迁移到Spark上来,S ...

  4. 【C】关键字void的用法

    void有两种功能 [1]没有 [2]任意类型 void出现的位置不同会有不同的解释 [1]void func( void ) func左边的void,代表『没有返回值』 func右边的括弧里的voi ...

  5. ubuntu安装cuda、cudnn

    环境: Ubuntu 16.04.4 LTS CUDA:8.0 CUDNN:5.1 CUDA下载:https://developer.nvidia.com/cuda-80-ga2-download-a ...

  6. 8-IdentityServer4登录中心

    1-新建webapi  IdentityServer4服务器项目 E:\coding\netcore\IdentityServerSample>dotnet new webapi --name ...

  7. OI生涯回忆录(二)

    (二)NOIP2016之后到HLOI2017 之后变得有点颓废,因为有的地方难度上来了,碰见不会的题我就会放挺.又或者有时候题水,改完了就不思进取了.到了过年前那几天连着考了几天试,好像是长春那边冬令 ...

  8. 成都Uber优步司机奖励政策(1月29日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. 封装Excls数据导出功能 返回一个下载链接地址

    /// <summary> /// 获取本地存储地址 /// </summary> /// <param name="dt"></para ...

  10. 必读的 Android 文章

    必读的 Android 文章 掘金官方 关注 2017.06.07 13:58* 字数 25218 阅读 8782评论 2喜欢 218 写给 Android 开发者的混淆使用手册 - Android ...