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. LeetCode--003--无重复字符的最长子串(java)

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc&qu ...

  2. Weighted Channel Dropout for Regularization of Deep Convolutional Neural Network

    这是AAAI2019的一篇论文,主要是为了解决小数据集的过拟合问题,使用了针对于卷积层的Dropout的方法. 论文的要点记录于下: 1.在训练过程中对于卷积层的channels进行droipout, ...

  3. JS中循环逻辑和判断逻辑的使用实例

    源代码见: https://github.com/Embrace830/JSExample &&和||的理解 a || b:如果a是true,那么b不管是true还是false,都返回 ...

  4. Fox And Dinner CodeForces - 510E (最大流)

    大意: n只狐狸, 要求分成若干个环, 每个环的狐狸不少于三只, 相邻狐狸年龄和为素数. 狐狸年龄都>=2, 那么素数一定为奇数, 相邻必须是一奇一偶, 也就是一个二分图, 源点向奇数点连容量为 ...

  5. 00-自测4. Have Fun with Numbers

    Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...

  6. 二元谓词中添加const的问题(未解决)

    #include <iostream> using namespace std; #include"set" #include"algorithm" ...

  7. server library[unbound] 服务未绑定解决办法

    情景如下:

  8. NOIP2015神奇的幻方

    题目描述 幻方是一种很神奇的 N∗N 矩阵:它由数字1,2,3,⋯⋯,N×N 构成,且每行.每列及两条对角线上的数字之和都相同. 当 N 为奇数时,我们可以通过下方法构建一个幻方: 首先将 1 写在第 ...

  9. 2015-09-21 css学习1

    3.设置背景图片 Background-image:url(相对路径) ----123.jpg 图片拉伸铺满: background-size:cover 铺满方向: background-repea ...

  10. LSTM UEBA异常检测——deeplog里其实提到了,就是多分类LSTM算法,结合LSTM预测误差来检测异常参数

    结合CNN的可以参考:http://fcst.ceaj.org/CN/article/downloadArticleFile.do?attachType=PDF&id=1497 除了行为,其他 ...