博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF 1003D Coins and Queries【位运算/硬币值都为2的幂/贪心】
阅读量:5291 次
发布时间:2019-06-14

本文共 2965 字,大约阅读时间需要 9 分钟。

Polycarp has n coins, the value of the i-th coin is ai. It is guaranteed that all the values are integer powers of 2 (i.e. ai=2d for some non-negative integer number d).

Polycarp wants to know answers on q queries. The j-th query is described as integer number bj. The answer to the query is the minimum number of coins that is necessary to obtain the value bj using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value bj, the answer to the j-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 n and q (1≤n,q≤2⋅105) — the number of coins and the number of queries.

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

The next q lines contain one integer each. The j-th line contains one integer bj — the value of the j-th query (1≤bj≤109).

Output

Print q integers ansj. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value bj the answer to the j-th query is -1.

Example

Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define debug() puts("++++")#define gcd(a,b) __gcd(a,b)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define fi first#define se second#define pb push_back#define sqr(x) ((x)*(x))#define ms(a,b) memset(a,b,sizeof(a))#define sz size()#define be begin()#define pu push_up#define pd push_down#define cl clear()#define lowbit(x) -x&x#define all 1,n,1#define rep(i,x,n) for(int i=(x); i<(n); i++)#define in freopen("in.in","r",stdin)#define out freopen("out.out","w",stdout)using namespace std;typedef long long ll;typedef unsigned long long ULL;typedef pair
P;const int INF = 0x3f3f3f3f;const ll LNF = 1e18;const int N = 1e7 + 20;const int maxm = 1e6 + 10;const double PI = acos(-1.0);const double eps = 1e-8;const int dx[] = {-1,1,0,0,1,1,-1,-1};const int dy[] = {0,0,1,-1,1,-1,1,-1};int dir[4][2] = { {0,1},{0,-1},{-1,0},{1,0}};const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};/*n个硬币,q次询问。第二行给你n个硬币的值(保证都是2的次幂)每次询问组成x块钱至少需要多少硬币*/int n,q;int a[N]; //之前1e3RE..map
m;int x;int main(){ while(~scanf("%d%d",&n,&q)) { int t; m.clear(); rep(i,0,n) { scanf("%d",&a[i]); m[a[i]]++; } while(q--) { int ans = 0; scanf("%d",&x); //2^30 2^29 ... 2^3=8 for(int i=(1<<30); i>=1; i/=2) //由大向小贪心 枚举硬币值(二次方 { int t = min(m[i], x/i); //选择需要的个数和有的个数的较小数 ans += t; x -= t * i; //cout<<"i="<
<<" "<<"m[i]="<
<<" "<<"x/i="<

转载于:https://www.cnblogs.com/Roni-i/p/9348667.html

你可能感兴趣的文章
关于数据库分布式架构的一些想法。
查看>>
BigDecimal
查看>>
Python语法基础之DataFrame
查看>>
Python语法基础之对象(字符串、列表、字典、元组)
查看>>
大白话讲解 BitSet
查看>>
sql语句中where与having的区别
查看>>
Python数据分析入门案例
查看>>
0x7fffffff的意思
查看>>
Java的值传递和引用传递
查看>>
HTML5的服务器EventSource(server-sent event)发送事件
查看>>
vue-devtools 获取到 vuex store 和 Vue 实例的?
查看>>
检查 chrome 插件是否存在
查看>>
在mac中,npm安装或者卸载失败,提示没有权限
查看>>
155. Min Stack
查看>>
亚稳态的产生机理、消除办法 (可以理解为什么打拍)
查看>>
<每日 1 OJ> -Table
查看>>
<每日 1 OJ> -LeetCode 7. 整数反转
查看>>
<每日 1 OJ> -LeetCode 13 . 罗马数字转正数
查看>>
c语言用指针定义一个类型进行输入输出
查看>>
数字电路基础知识
查看>>