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 ...
随机推荐
- A1043. Is It a Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 将vcf文件转化为plink格式并且保持phasing状态
VCFtools can convert VCF files into formats convenient for use in other programs. One such example i ...
- java键盘中输入q退出
用 java.util.Scanner 如下 public static void main(String[] args) { while(true){ Scanner scan = new Scan ...
- PostCSS理解与运用
1.PostCSS是什么 它可以被理解为一个平台,可以让一些插件在上面跑 它提供了一个解析器,可以将CSS解析成抽象语法树 通过PostCSS这个平台,我们能够开发一些插件,来处理CSS.热门插件如a ...
- Tomcat 一般异常处理方式
1.启动时出现: Exception starting filter struts2 java.lang.NoClassDefFoundError: org/apache/commons/lang3/ ...
- Qt ------ QFileDialog
QString strFile = QFileDialog::getOpenFileName(this,QStringLiteral("选择Excel文件"),"&quo ...
- opencv源码学习: getGaussianKernel( 高斯核);
参考: https://blog.csdn.net/u012633319/article/details/80921023 二维高斯核, 可以根据下面的公式推到为两个一维高斯核的乘积: 原型: /** ...
- ansible-playbook 单个yml文件部署tomcat简单示例
#单yaml配置[root@jenkins pb]# cat tomcat.yml --- - hosts: eee vars: #设置变量 war_files: /var/lib/jenkins/w ...
- 打包JAR,MANIFEST.MF格式
MANIFEST.MF文件格式详解 1. 基本格式 属性名称+:+空格+属性值 2. 没行最多72个字符,换行继续必须以空格开头 3. 文件最后必须要有一个回车换行 4. Class-Path 当前路 ...
- java io系列11之 FilterOutputStream
FilterOutputStream 介绍 FilterOutputStream 的作用是用来“封装其它的输出流,并为它们提供额外的功能”.它主要包括BufferedOutputStream, Dat ...