Coins and Queries
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp has nn coins, the value of the ii-th coin is aiai. It is guaranteed that all the values are integer powers of 22 (i.e. ai=2dai=2d for somenon-negative integer number dd).

Polycarp wants to know answers on qq queries. The jj-th query is described as integer number bjbj. The answer to the query is the minimum number of coins that is necessary to obtain the value bjbj using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value bjbj, the answer to the jj-th query is -1.

The queries are independent (the answer on the query doesn't affect Polycarp's coins).

Input

The first line of the input contains two integers nn and qq (1≤n,q≤2⋅1051≤n,q≤2⋅105) — the number of coins and the number of queries.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an — values of coins (1≤ai≤2⋅1091≤ai≤2⋅109). It is guaranteed that all aiai are integer powers of 22 (i.e. ai=2dai=2d for some non-negative integer number dd).

The next qq lines contain one integer each. The jj-th line contains one integer bjbj — the value of the jj-th query (1≤bj≤1091≤bj≤109).

Output

Print qq integers ansjansj. The jj-th integer must be equal to the answer on the jj-th query. If Polycarp can't obtain the value bjbj the answer to the jj-th query is -1.

Example
input

Copy
5 4
2 4 8 2 4
8
5
14
10
output

Copy
1
-1
3
2 题意: 给你n个硬币,每个硬币的价值是2的幂数,然后接下来有m次查询,每次查询问这个数值最少需要多少枚硬币能凑出来? 分析: 我们直接从可能的最大的2的幂数开始枚举,遇到比数值小的就加上,注意每种硬币肯恩有多个,我们加的时候得看所需要加的个数。经过分析可以知道我们要加的是这种硬币的个数和数值所需要的最多这种硬币个数的最小值
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 2e5 + ;
const int mod = 1e9 + ;
typedef long long ll;
int main() {
ll n, m;
while( cin >> n >> m ) {
map<ll,ll> mm;
for( ll i = ; i < n; i ++ ) {
ll a;
cin >> a;
mm[a] ++;
}
while( m -- ) {
ll t;
cin >> t;
ll ans = ;
for( ll i = <<; i >= ; i /= ) {
ans += min( mm[i], t/i );
t -= min( mm[i], t/i ) * i;
}
if( t ) {
cout << - << endl;
} else {
cout << ans << endl;
}
}
}
return ;
}

CF1003D Coins and Queries 贪心的更多相关文章

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

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

  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(贪心

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

  4. Coins and Queries(map迭代器+贪心)

    题意 n个硬币,q次询问.第二行给你n个硬币的面值(保证都是2的次幂!).每次询问组成b块钱,最少需要多少个硬币? Example Input 5 42 4 8 2 4851410 Output 1- ...

  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. $POJ1742\ Coins$ 多重背包+贪心

    Vjudge传送门 $Sol$ 首先发现这是一个多重背包,所以可以用多重背包的一般解法(直接拆分法,二进制拆分法...) 但事实是会TLE,只能另寻出路 本题仅关注“可行性”(面值能否拼成)而不是“最 ...

  7. codeforces1003D(贪心)

    D. Coins and Queries time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. 7.24-Codeforces Round #494 (Div. 3)

    链接:http://codeforces.com/contest/1003 A. Polycarp's Pockets 题型:模拟 题意:把初始集合拆分,要求相同的数不在同一个集合中,求出需要的集合个 ...

  9. Codeforces Round #494 (Div 3) (A~E)

    目录 Codeforces 1003 A.Polycarp's Pockets B.Binary String Constructing C.Intense Heat D.Coins and Quer ...

随机推荐

  1. JAVA-Spring AOP五大通知类型

    一.前置通知 在目标方法执行之前执行的通知 在前置通知方法,可以没有参数,也可以额外接收一个JoinPoint,Spring会自动将该对象传入,代表当前的连接点,通过该对象可以获取目标对象和目标方法相 ...

  2. 基于Spring框架应用的权限控制系统的研究和实现

    摘 要: Spring框架是一个优秀的多层J2EE系统框架,Spring本身没有提供对系统的安全性支持.Acegi是基于Spring IOC 和 AOP机制实现的一个安全框架.本文探讨了Acegi安全 ...

  3. 你所不知道的 CSS 负值技巧与细节

    写本文的起因是,一天在群里有同学说误打误撞下,使用负的 outline-offset 实现了加号.嗯?好奇的我马上也动手尝试了下,到底是如何使用负的 outline-offset 实现加号呢? 使用负 ...

  4. LeetCode——409. Longest Palindrome

    题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest ...

  5. 带你剖析WebGis的世界奥秘----瓦片式加载地图

    WebGIS应用程序的页面能够通过HTML.JSP.ASP或任何任何类型的Web页文件构成,其特殊之处在于,它的请求提交的方法并不是通过常用的 "超链接"形式,而是使用鼠标与Web ...

  6. 安利一个免费下载VIP文档神器

    今天安利给大伙一个非非非常好用的可以免费下载VIP文档的下载神器------冰点文库下载器,用过的人都说好.操作简单,小巧轻便,完全免费.支持百度.豆丁.畅享.mbalib.hp009.max.boo ...

  7. Oracle中的日期函数

    (一)查询系统的当前日期用sysdate,用法如下: select sysdate from dual 日期操作的三个格式: 日期-数字=日期 日期+=日期 日期-日期=数字(天数) (二)常用的日期 ...

  8. 【win10主机】连接virtualbox上【32位winXP系统虚拟机】上启动的mysql

    问题Q: 在virtualbox上启动winXP系统虚拟机后,启动含oa项目的tomcat,数据库服务也运行起来了,虚拟机上连接无误: 在上一篇<主机访问 虚拟机启动的项目>基础上,尝试连 ...

  9. 朴素贝叶斯python代码实现(西瓜书)

    朴素贝叶斯python代码实现(西瓜书) 摘要: 朴素贝叶斯也是机器学习中一种非常常见的分类方法,对于二分类问题,并且数据集特征为离散型属性的时候, 使用起来非常的方便.原理简单,训练效率高,拟合效果 ...

  10. Sqlserver2012 评估期已过问题

    sql server 2012提示评估期已过的解决方法: 第一步:进入SQL2012配置工具中的安装中心. 第二步:再进入左侧维护选项界面,然后选择选择版本升级. 第三步:进入输入产品密钥界面,输入相 ...