D. 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 some non-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指数次方的硬币,然后有q个问题,每次给出一个数值,问是否能用最少的所给出的硬币组成这个面值,能组成则输出最小值,不能输出-1.

思路:能用大面值的就用大面值的

代码:

#include<stdio.h>
#include<map>
using namespace std;
map<int,int> mp;
struct{
int sm;
}st[35];
int mn;
int s[35];
void init(){
int i,a;
a=1;
mp[1]=0;
st[0].sm=1;
for(i=1;i<=31;i++){
a=a*2;
mp[a]=i;
st[i].sm=a;
}
}
int min(int a,int b){
if(a<b)
return a;
return b;
}
int dfs(int k,int j,int cnt){
int i;
if(j<0){
if(k==0)
mn=cnt;
return 0;
}
i=min(k/st[j].sm,s[mp[st[j].sm]]);
dfs(k-i*st[j].sm,j-1,cnt+i);
return 0;
}
int main(){
init();
int n,q;
int i,j,a;
scanf("%d%d",&n,&q);
for(i=0;i<n;i++){
scanf("%d",&a);
s[mp[a]]++;
}
while(q--){
mn=0;
scanf("%d",&a);
dfs(a,31,0);
if(mn==0)
printf("-1\n");
else
printf("%d\n",mn);
}
return 0;
}

  

codeforces1003D(贪心)的更多相关文章

  1. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  2. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

  7. code vs 1098 均分纸牌(贪心)

    1098 均分纸牌 2002年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 有 N 堆纸牌 ...

  8. 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

    SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...

  9. 【贪心】HDU 1257

    HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...

随机推荐

  1. MapReduce处理气象数据

    老师:MissDu 提交作业 1. 用Python编写WordCount程序并提交任务 程序 WordCount 输入 一个包含大量单词的文本文件 输出 文件中每个单词及其出现次数(频数),并按照单 ...

  2. 在linux中要直接查找的命令

  3. day1-6 字符串、列表、元组、字典、类型转换

    day1 1.python历史. 宏观上:python2 与 python3 区别: python2 源码不标准,混乱,重复代码太多, python3 统一 标准,去除重复代码. 2.python的环 ...

  4. 23. Merge K Sorted Lists (Java, 归并排序的思路)

    题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  5. GPLT L2-004 这是二叉搜索树吗?

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805070971912192 类似题目有FBI树 这两个题有个小 ...

  6. CSS Hack 技巧

    IE Hack IE系列浏览器的hack大略如下: _nowamagic:1px;———–ie6 *nowamagic:1px;———–ie7 nowamagic:1px\0;———-ie89 now ...

  7. hdu-4738-tarjin/割边

    http://acm.hdu.edu.cn/showproblem.php?pid=4738 求得是边权最小的割边,和求割点类似用tarjin,但要注意的是不能走从父亲过来的那一条边,在割点里那样理解 ...

  8. sharding-jdbc读写分离原理解读

    原帖地址:https://blog.csdn.net/yanyan19880509/article/details/78170233 前言 很多时候,为了应付DB的高并发读写,我们会采用读写分离技术. ...

  9. Windows下Python3.6安装第三方模块

    一 安装pip 一般需要用pip进行安装,不过我安装p3.6的时候pip已经有了. 如果没有的话,可以用在以下http://www.lfd.uci.edu/~gohlke/pythonlibs/#pi ...

  10. Oracle EBS SLA 详解(转)

    原文地址: Oracle EBS SLA 详解