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 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<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#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<int,int> 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<int,int> 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="<<i<<" "<<"m[i]="<<m[i]<<" "<<"x/i="<<x/i<<" "<<"t="<<t<<" "<<"ans="<<ans<<" "<<"x="<<x<<endl;
}
if(x)
{
printf("-1\n");
}
else cout<<ans<<endl;
}
}
}
/*
5 4
2 4 8 2 4
8
5
14
10
1
-1
3
2
*/
CF 1003D Coins and Queries【位运算/硬币值都为2的幂/贪心】的更多相关文章
- 关于C/C++中的位运算技巧
本篇文章讲述在学习CSAPP位运算LAB时的一些心得. 移位运算的小技巧 C/C++对于移位运算具有不同的策略,对于无符号数,左右移位为逻辑移位,也就是直接移位:对于有符号数,采用算术移位的方式,即左 ...
- 【BZOJ3668】[NOI2014] 起床困难综合症(位运算思想)
点此看题面 大致题意: 给定一些位运算操作,让你在\(0\sim m\)范围内选一个初始值,使其在经过这些运算后得到的结果最大. 前置技能:关于位运算 作为一道位运算的题,如果你不知道什么是位运算,那 ...
- Java 位运算超全面总结
1.原码.反码.补码 关于原码.反码.补码的相关知识作者不打算在这里长篇大论,相关知识已有别的大佬总结很好了,还请老铁自行 Google,不过有篇知乎回答是作者学编程以来见过对相关知识最通俗易懂,生动 ...
- 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 ...
- JAVA:二进制(原码 反码 补码),位运算,移位运算,约瑟夫问题(5)
一.二进制,位运算,移位运算 1.二进制 对于原码, 反码, 补码而言, 需要注意以下几点: (1).Java中没有无符号数, 换言之, Java中的数都是有符号的; (2).二进制的最高位是符号位, ...
- Java学习第五篇:二进制(原码 反码 补码),位运算,移位运算,约瑟夫问题
一.二进制,位运算,移位运算 1.二进制 对于原码, 反码, 补码而言, 需要注意以下几点: (1).Java中没有无符号数, 换言之, Java中的数都是有符号的; (2).二进制的最高位是符号位, ...
- Vus the Cossack and Strings(Codeforces Round #571 (Div. 2))(大佬的位运算实在是太强了!)
C. Vus the Cossack and Strings Vus the Cossack has two binary strings, that is, strings that consist ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- 简简单单学会C#位运算
一.理解位运算 要学会位运算,首先要清楚什么是位运算?程序中的所有内容在计算机内存中都是以二进制的形式储存的(即:0或1),位运算就是直接对在内存中的二进制数的每位进行运算操作 二.理解数字进制 上面 ...
随机推荐
- 【bzoj4129】Haruna’s Breakfast 带修改树上莫队+分块
题目描述 给出一棵树,点有点权.支持两种操作:修改一个点的点权,查询链上mex. 输入 第一行包括两个整数n,m,代表树上的结点数(标号为1~n)和操作数.第二行包括n个整数a1...an,代表每个结 ...
- 2017 Multi-University Training Contest - Team 2 TrickGCD(组合数学)
题目大意: 给你一个序列An,然后求有多少个序列Bn 满足Bi<=Ai,且这个序列的gcd不为1 题解: 考虑这样做 枚举一个因子k,然后求出有多少个序列的gcd包含这个因子k 然后把结果容斥一 ...
- 【题解】HAOI2008硬币购物
1A什么的实在是太开心啦~~洛谷P1450 这道题目主要是考察对于容斥原理的掌握. 首先,注意到如果不存在有关硬币数量的限制而单纯询问方案的总数,就是一个简单的完全背包.这个思路提醒我们:如果能够求出 ...
- [Leetcode] Path Sum II路径和
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- BZOJ2120 数颜色 【带修改莫队】
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MB Submit: 6579 Solved: 2625 [Submit][Status][Discus ...
- 【NOIP模拟赛】天神下凡 动态开点线段树
这些圆一定是在同一水平面上的,由于他们没有相交,因此我们发现他们每个人与外界关系可以分为,1.存在并圈圈 2.存在圈圈并被割,因此我们把所有的圆都加1,把被割的在加1,就可以啦,因此我们开一个线段树, ...
- HDU3605:Escape(状态压缩+最大流)
Escape Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- Educational Codeforces Round 54 (Rated for Div. 2) ABCD
A. Minimizing the String time limit per test 1 second memory limit per test 256 megabytes Descriptio ...
- 如何在plsql/developer的命令窗口执行sql脚本
在plsql/developer的命令窗口执行sql脚本的命令是@+路径 示例如下: 第一步:在C:\Users\linsenq\Desktop目录下新建一个脚本文件: test.sql test.s ...
- json获取属性值的方式
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript(Standard ECMA-262 ...