题目链接

题目大意:给你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. react-native中的style

    在 React Native 中,你并不需要学习什么特殊的语法来定义样式.我们仍然是使用 JavaScript 来写样式. 所有的核心组件都接受名为style的属性.这些样式名基本上是遵循了 web ...

  2. request 请求头的处理

    一.请求头:说明了请求要带的一些说明,有的请求需要带,有的不需要带 一般会带上格式,对于新浪微博来说,他一定要带上user-agent content-type:application/json 二. ...

  3. http请求流程

  4. poj 2559(栈的应用)

    传送门 参考资料: [1]:挑战程序设计竞赛 题意: 柱状图是由一些宽度相等的长方形下端对齐后横向排列得到的图形.现在有由n个宽度为1,高度分别为h[1,2,3.......n]的长方形从左到右依次排 ...

  5. linux 进程创建clone、fork与vfork

    目录: 1.clone.fork与vfork介绍 2.fork说明 3.vfork说明 4.clone说明5.fork,vfork,clone的区别 内容: 1.clone.fork与vfork介绍 ...

  6. tcpdump高级过滤

    一:查看帮助选项 tcpdump --help Usage: tcpdump [-aAbdDefhHIJKlLnNOpqStuUvxX#] [ -B size ] [ -c count ] [ -C ...

  7. opencv: 基本知识(二);

    1.cvArr* 与 cv::Mat之间的转换; cv::Mat ---- > cvArr* cv::Mat img; IplImage temp = IplImage(img); cvArr ...

  8. CodeChef - BLACKCOM 可行性dp转最优化树dp

    https://www.codechef.com/problems/BLACKCOM 题意:一颗5000个黑白结点的树,10W个查询寻找是否存在大小s并且有t和黑节点的子图 一开始就觉得应当是一个树d ...

  9. jsp+servlet+jdbc实现表单提交

    1.新建一个maven工程,选webapp模板 2.安装tomcat https://tomcat.apache.org/download-80.cgi 下载解压到自定义目录上 ps:在全局变量加上J ...

  10. linux下安装SlickEdit

    title: linux下安装SlickEdit tags: 软件 date: 2018-10-08 21:32:12 --- linux下安装SlickEdit 下载安装包和补丁文件 补丁文件 官方 ...