Codeforces Round #494 (Div. 3) D. Coins and Queries (贪心,数学)

题意:给你一组全是\(2^d\ (d\ge0)\)的数,询问q次,每次询问一个数,问这个数是否能够由原数组中的数相加得到,如果能,输出最少用多少个数,否则输出\(-1\).
题解:首先贪心得出结论:如果情况成立,那么最少的情况一定是优先用数组中大的数,然后我们用桶记录数组数的个数,从\(inf\)开始枚举,\(k\)表示桶中的数和\(x\)所需次数的最小值,最后如果\(x\ne 0\)那么条件不满足,否则输出\(ans\)即可.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL; int n,q;
int x;
int ans;
map<int,int> mp; int main() {
ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>q;
for(int i=1;i<=n;++i){
cin>>x;
mp[x]++;
} while(q--){
cin>>x;
ans=0;
for(int i=1<<30;i>=1;i/=2){
int k=min(mp[i],x/i);
ans+=k;
x-=k*i;
}
if(x) puts("-1");
else printf("%d\n",ans);
}
return 0;
}
Codeforces Round #494 (Div. 3) D. Coins and Queries (贪心,数学)的更多相关文章
- Codeforces Round #494 (Div. 3) D. Coins and Queries(贪心
题目链接 题目大意:给你n个物品,第iii个物品价值aia_iai,询问q次,问你能不能凑出价值为qiq_iqi的物品. 小贪心吧.从大到小找,能拿就拿就行了. #include<bits/ ...
- Codeforces Round #330 (Div. 1) A. Warrior and Archer 贪心 数学
A. Warrior and Archer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/594 ...
- 递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries
题目传送门 /* 递推:用cnt记录前缀值,查询区间时,两个区间相减 */ #include <cstdio> #include <algorithm> #include &l ...
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #494 (Div 3) (A~E)
目录 Codeforces 1003 A.Polycarp's Pockets B.Binary String Constructing C.Intense Heat D.Coins and Quer ...
- Codeforces Round #494 (Div. 3)
刚好在考完当天有一场div3,就开了个小号打了,打的途中被辅导员喊去帮忙,搞了二十分钟-_-||,最后就出了四题,题解如下:题目链接:http://codeforces.com/contest/100 ...
- Codeforces Round #523 (Div. 2) A. Coins
A. Coins 题目链接:https://codeforc.es/contest/1061/problem/A 题意: 给出n和s,要在1-n中选数(可重复),问最少选多少数可以使其和为s. 题解: ...
- Codeforces Round #324 (Div. 2) E. Anton and Ira 贪心
E. Anton and Ira Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/probl ...
- Codeforces Round #404 (Div. 2) D. Anton and School - 2 数学
D. Anton and School - 2 题目连接: http://codeforces.com/contest/785/problem/D Description As you probabl ...
随机推荐
- MoChat - 国内首款完全开源的 PHP 企业微信管理系统正式发布
MoChat -- 让企业微信开发更简单 项目地址 Github: https://github.com/mochat-cloud/mochat Gitee: https://gitee.com/mo ...
- 【Oracle】修改列的大小
alter table 表名 modify column_name varchar2(32) alter table 表名 modify (column_name1 varchar(20) defa ...
- 【Oracle】生成随机数
Oracle生成随机数: dbms_random.string(opt, 6) --括号里的opt要从下面的列表中选择,数字代表要生成几位随机数,如果是1位的话,就改成1 以此类推 opt可取 ...
- kubernets之Replication Controller
一 Replication Controller的介绍 pod可能会由于各种原因消失和多出来,例如node节点去除集群或者人为的手工创建,所以为了方便和管理pod的数量,k8s里面 的另外 ...
- 爬虫+django,打造个性化API接口
简述 今天也是同事在做微信小程序的开发,需要音乐接口的测试,可是用网易云的开放接口比较麻烦,也不能进行测试,这里也是和我说了一下,所以就用爬虫写了个简单网易云歌曲URL的爬虫,把数据存入mysql数据 ...
- JavaScript小记
JavaScript小记 1. 简介 1. 语言描述 JavaScript 是一门跨平台.面向对象的弱类型动态脚本编程语言 JavaScript 是一门基于原型.函数先行的语言 JavaScript ...
- Py-re正则模块,log模块,config模块,哈希加密
9.re正则表达式模块,用于字符串的模糊匹配 元字符: 第一:点为通配符 用.表示匹配除了换行符以外的所有字符 import re res=re.findall('a..x','adsxwassxdd ...
- mybatis缓存源码分析之浅谈缓存设计
本文是关于mybatis缓存模块设计的读后感,关于缓存的思考,关于mybatis的缓存源码详细分析在另一篇文章:https://www.cnblogs.com/gmt-hao/p/12448896.h ...
- OpenDaylight — YANG
1. 介绍 YANG 是一种用于为 NETCONF 协议建模数据的语言. YANG 将数据的层次结构建模为一棵树. 2. 节点类型 2.1 leaf 它只有一个特定类型的值,并且没有子节点. YANG ...
- 【python刷题】LRU
什么是LRU? LRU是Least Recently Used的缩写,即最近最少使用,是一种常用的页面置换算法,选择最近最久未使用的页面予以淘汰.该算法赋予每个页面一个访问字段,用来记录一个页面自上次 ...