Codeforces Round #494 (Div. 3) D. Coins and Queries(贪心
题目链接
题目大意:给你n个物品,第iii个物品价值aia_iai,询问q次,问你能不能凑出价值为qiq_iqi的物品。
小贪心吧。从大到小找,能拿就拿就行了。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
const int N = 2e5 +11;
int n,q;
int a[N],s[N];
LL sum[N];
int cnt[434],mid[343];
int main(){
ios::sync_with_stdio(false);
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
int now=0;
while(a[i]!=1){
now++;
a[i]/=2;
}
cnt[now]++;
}
for(int i=1;i<=q;i++){
int t;
cin>>t;
int ans=0;
for(int j=32;j>=0;j--){
mid[j]=cnt[j];
if((1ll<<j)>t)continue;
if(mid[j]==0)continue;
int cnt=t/(1ll<<j);
cnt=min(cnt,mid[j]);
ans+=cnt;
t-=(1ll<<j)*cnt;
}
if(t)cout<<-1<<endl;
else cout<<ans<<endl;
}
return 0;
}
Codeforces Round #494 (Div. 3) D. Coins and Queries(贪心的更多相关文章
- Codeforces Round #494 (Div. 3) D. Coins and Queries (贪心,数学)
题意:给你一组全是\(2^d\ (d\ge0)\)的数,询问q次,每次询问一个数,问这个数是否能够由原数组中的数相加得到,如果能,输出最少用多少个数,否则输出\(-1\). 题解:首先贪心得出结论:如 ...
- 递推 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 #329 (Div. 2)B. Anton and Lines 贪心
B. Anton and Lines The teacher gave Anton a large geometry homework, but he didn't do it (as usual ...
- Codeforces Round #370 (Div. 2)C. Memory and De-Evolution 贪心
地址:http://codeforces.com/problemset/problem/712/C 题目: C. Memory and De-Evolution time limit per test ...
随机推荐
- ONI无法启动: Uh oh! Unable to launch Neovim...
问题描述 在终端中是可以打开nvim的,ONI无法正确找到位置 解决方法 修改配置文件,指定nvim的路径 终端中输入which nvim定位所在位置,这里返回的结果是/usr/local/bin/n ...
- [luogu3377][左偏树(可并堆)]
题目链接 思路 左偏树的模板题,参考左偏树学习笔记 对于这道题我是用一个并查集维护出了哪些点是在同一棵树上,也可以直接log的往上跳寻找根节点 代码 #include<cstdio> #i ...
- 关于在 java 8 下开启 TLS_RSA_WITH_3DES_EDE_CBC_SHA 支持 xp ie8 tls1.0 的正常访问
最近为 aioserver 增加了ssl支持. 在 myssl.com 上测试了一下,关于[客户端握手模拟]发现 ie8 xp tls1.0 这一项提示:握手失败 (服务器断开连接) 我又试了一下 ...
- (五)Oracle 的 oracle 表查询
http://www.hechaku.com/Oracle/oracle_tables_chack.html 通过scott用户下的表来演示如何使用select语句,接下来对emp.dept.salg ...
- django2+uwsgi+nginx上线部署到服务器Ubuntu16.04(最新最详细版)
1.前期准备 1.打开Terminal终端,执行以下命令,将项目所需要的依赖包,都记录到一个文件内备用. pip freeze >requirements.txt 2.将项目文件夹→右键→添加压 ...
- Day16--Python--初识面向对象
今日主要内容一. 面向对象思想 1. 面向过程. 重点在'过程'. 按照事物迭代发展流程. 优点: 简单,流水线式的操作 缺点: 可扩展性差.可维护性差. 2. 面向对象. 对象是属性和动作的集合体. ...
- JS模块化开发(四)——构建工具gulp
gulp.js——基于流的自动化构建工具 步骤: 1. 全局安装 gulp: $ npm install --global gulp 2. 作为项目的开发依赖(devDependencies)安装: ...
- bigdata learning unit two--Spark environment setting
1.下载 Spark安装之前的准备 文件的解压与改名 tar -zxvf spark-2.2.0-bin-hadoop2.7.tgz rm -rf spark-2.2.0-bin-hadoop2.7. ...
- jsp+servlet+poi导出数据库中的数据
index.jsp: <a href="POIout">简单导出数据</a> <a href="POIoutTemplate"&g ...
- layui获取子集表单中的值,关闭父级弹窗
js代码 var GetParams = function(url,bool) { try { if(bool){ var index = url.indexOf('?'); url = url.ma ...