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. 【消息队列】从各方面比较下kafka、activemq、rabbitmq、rocketmq之间的区别

    一.单机吞吐量ActiveMQ:万级,吞吐量比RocketMQ和Kafka要低了一个数量级RabbitMQ:万级,吞吐量比RocketMQ和Kafka要低了一个数量级RocketMQ:10万级,Roc ...

  2. Spring Boot 获得帮助

    如果你在使用 Spring Boot 的时候遇到了问题,我们很乐意为你提供帮助. 请访问 IX. How-to指南 中的内容 — 在这个指南中为常见的多数问题提供了解决方案. 学习更多有关 Sprin ...

  3. Fragment的onCreateView和onActivityCreate之间的区别(转)

    看了有关这个问题的几篇博文,几乎都是引用了stackoverflow上的一个回答: 问题: I know that a fragment’s view hierarchy has to be infl ...

  4. 先天性肾上腺增生症(ACH)

    先天性肾上腺增生症 类型 症状 并发症 治疗 情感支持 产前筛查 预防 什么是先天性肾上腺皮质增生症? 先天性肾上腺增生症(CAH)是一组影响肾上腺的遗传性疾病.肾上腺产生激素皮质醇和醛固酮.CAH是 ...

  5. Codeforces Round #503 (by SIS, Div. 1)E. Raining season

    题意:给一棵树每条边有a,b两个值,给你一个m,表示从0到m-1,假设当前为i,那么每条边的权值是a*i+b,求该树任意两点的最大权值 题解:首先我们需要维护出(a,b)的凸壳,对于每个i在上面三分即 ...

  6. python-flask基本应用模板

    1.模板继承 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  7. Echarts 简单报表系列三:饼状图

    代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. Spring注解之BeanPostProcessor与InitializingBean

    /*** BeanPostProcessor 为每个bean实例化时提供个性化的修改,做些包装等*/ package org.springframework.beans.factory.config; ...

  9. set集合深浅拷贝以及知识补充

    一. 对之前的知识点进行补充. 1. str中的join方法. 把列表转换成字符串 li = ["李嘉诚", "麻花藤", "黄海峰", & ...

  10. Caused by: java.io.FileNotFoundException: class path resource [spring/springmvc.xml] cannot be opene

                        Caused by: java.io.FileNotFoundException: class path resource [spring/springmvc. ...