题目链接

题目大意:给你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(贪心的更多相关文章

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

    题意:给你一组全是\(2^d\ (d\ge0)\)的数,询问q次,每次询问一个数,问这个数是否能够由原数组中的数相加得到,如果能,输出最少用多少个数,否则输出\(-1\). 题解:首先贪心得出结论:如 ...

  2. 递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries

    题目传送门 /* 递推:用cnt记录前缀值,查询区间时,两个区间相减 */ #include <cstdio> #include <algorithm> #include &l ...

  3. 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  ...

  4. Codeforces Round #494 (Div 3) (A~E)

    目录 Codeforces 1003 A.Polycarp's Pockets B.Binary String Constructing C.Intense Heat D.Coins and Quer ...

  5. Codeforces Round #494 (Div. 3)

    刚好在考完当天有一场div3,就开了个小号打了,打的途中被辅导员喊去帮忙,搞了二十分钟-_-||,最后就出了四题,题解如下:题目链接:http://codeforces.com/contest/100 ...

  6. Codeforces Round #523 (Div. 2) A. Coins

    A. Coins 题目链接:https://codeforc.es/contest/1061/problem/A 题意: 给出n和s,要在1-n中选数(可重复),问最少选多少数可以使其和为s. 题解: ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 洛谷P2704 炮兵阵地

    本题过于经典...... 对于这种网格状压DP,套路一波刷表法DFS转移就没了. 三进制状压,0表示当前,上一个都没有.1表示当前无,上一个有.2表示当前有. 转移的条件就是上一行为0,当前不是山地, ...

  2. MariaDB安装及基本配置

    MariaDB安装及基本配置(CentOS6.9) 数据库基础概念 数据库(Database, DB)是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库. DBMS: Database ...

  3. (转)Java并发编程:线程池的使用

    背景:线程池在面试时候经常遇到,反复出现的问题就是理解不深入,不能做到游刃有余.所以这篇博客是要深入总结线程池的使用. ThreadPoolExecutor的继承关系 线程池的原理 1.线程池状态(4 ...

  4. Django(五)框架之模板继承和静态文件配置

    https://www.cnblogs.com/haiyan123/p/7731959.html 一.模板继承 目的是:减少代码的冗余 语法: {% block classinfo %} {% end ...

  5. jQuery 传递对象参数到Spring Controller

    当jQuery 发送ajax请求需要传递多个参数时,如果参数过多,Controller接收参数时就需要定义多个参数,这样接口方法会比较长,也不方便.Spring可以传递对象参数,将你需要的所有查询条件 ...

  6. MySQL STR_TO_DATE函数

    转: MySQL STR_TO_DATE函数 2017年12月05日 15:41:58 木林森淼 阅读数:23822   版权声明:水平有限,如有补充或更正,望大家评论指正 https://blog. ...

  7. Java多线程-详细版

    基本概念解释 并发:一个处理器处理多个任务,这些任务对于处理器来说是交替运行的,每个时间点只有一个任务在进行. 并行:多个处理器处理多个任务,这些任务是同时运行的.每个时间点有多个任务同时进行. 进程 ...

  8. Mac 上有哪些鲜为人知且极大提高效率的工具?

    来源:知乎文章收录于:风云社区SCOEE,提供上千款各类mac软件下载 1. Focus  功能: 屏蔽影响你学习的网站.  同类软件:Self Control, Rescue Time  特点 ...

  9. Go-day03

    概要: 1.strings与strconv的使用 2.Go中的时间和日期类型 3.流程控制 4.函数详解 strings与strconv用法 1.strings.HasPrefix(s string, ...

  10. mysql 将一张表的数据更新到另外一张表中

    update 更新表 set 字段 = (select 参考数据 from 参考表 where  参考表.id = 更新表.id); update table_2 m  set m.column = ...