题目链接

题目大意:给你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. CF235D Graph Game

    CF235D Graph Game 好题 树? 考虑每个点被计算多少次 但是和当前分治中心有关系的 所以,f(a,b),对于a作为中心时候,和b相连的概率 也就是两者必然分离,最后一次连在一起的时候, ...

  2. vetur插件提示 'v-for' directives require 'v-bind:key' directives.错误的解决办法

    在用vscode编写vue代码时,因为安装的有vetur插件,所以当代码中有v-for语法时,会提示 [vue-language-server] 'v-for' directives require ...

  3. luogu2643 聪聪可可

    题目链接 题意 其实转化之后的题意就是求出树上有多少条路径长度是3的倍数.求答案的时候只要将这个数字除以总路径数量就行了. 思路 考虑点分治.对于当前子树,分别求出出树中每个点到根的路径长度对\(3\ ...

  4. POJ3734Blocks(递推+矩阵快速幂)

    题目链接:http://poj.org/problem?id=3734 题意:给出n个排成一列的方块,用红.蓝.绿.黄四种颜色给它们染色,求染成红.绿的方块个数同时为偶数的方案数模10007的值. 题 ...

  5. python 二维数组读入

    study from : https://www.cnblogs.com/reaptomorrow-flydream/p/9613847.html python 二维数组键盘输入 1 m = int( ...

  6. PMP项目管理考试培训机构内部资料打包赠送(3个PPT)

    PMP认证考试我自己这边是今年6月份考过了的,手里觉得对自己有帮助的资料就是这3个PPT,讲解的比较清晰,知识点详细.结合自己做的笔记,备考十分轻松.所以推荐大家也看一下. 有需要的可以联系. PPT ...

  7. struts2 OGNL配和通用标签和其它标签的使用

    三.OGNL配合通用标签的其他使用 1.iterator标签(很重要) 动作类 package com.itheima.web.action; import java.util.ArrayList; ...

  8. TestNg 4.组测试中的方法分组测试

    看以下代码: package com.course.testng.groups; import org.testng.annotations.AfterGroups; import org.testn ...

  9. asp.net 获得伪静态网址解决微信sdk签名问题

    手机网站是asp.net c#编写的,前几天因为要使用微信SDK在手机网站页面使用分享功能,但是程序使用了伪静态功能.如果原地址是:http://ww.xx.com/news/show.aspx?id ...

  10. logistics回归简单应用——梯度下降,梯度上升,牛顿算法(一)

    警告:本文为小白入门学习笔记 由于之前写过详细的过程,所以接下来就简单描述,主要写实现中遇到的问题. 数据集是关于80人两门成绩来区分能否入学: 数据集: http://openclassroom.s ...