codeforces1003D(贪心)
2 seconds
256 megabytes
standard input
standard output
Polycarp has nn coins, the value of the ii-th coin is aiai. It is guaranteed that all the values are integer powers of 22 (i.e. ai=2dai=2d for some non-negative integer number dd).
Polycarp wants to know answers on qq queries. The jj-th query is described as integer number bjbj. The answer to the query is the minimum number of coins that is necessary to obtain the value bjbj using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value bjbj, the answer to the jj-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
The first line of the input contains two integers nn and qq (1≤n,q≤2⋅1051≤n,q≤2⋅105) — the number of coins and the number of queries.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an — values of coins (1≤ai≤2⋅1091≤ai≤2⋅109). It is guaranteed that all aiai are integer powers of 22 (i.e. ai=2dai=2d for some non-negative integer number dd).
The next qq lines contain one integer each. The jj-th line contains one integer bjbj — the value of the jj-th query (1≤bj≤1091≤bj≤109).
Print qq integers ansjansj. The jj-th integer must be equal to the answer on the jj-th query. If Polycarp can't obtain the value bjbj the answer to the jj-th query is -1.
5 4
2 4 8 2 4
8
5
14
10
1
-1
3
2
题意:给你n个面值为2指数次方的硬币,然后有q个问题,每次给出一个数值,问是否能用最少的所给出的硬币组成这个面值,能组成则输出最小值,不能输出-1.
思路:能用大面值的就用大面值的
代码:
#include<stdio.h>
#include<map>
using namespace std;
map<int,int> mp;
struct{
int sm;
}st[35];
int mn;
int s[35];
void init(){
int i,a;
a=1;
mp[1]=0;
st[0].sm=1;
for(i=1;i<=31;i++){
a=a*2;
mp[a]=i;
st[i].sm=a;
}
}
int min(int a,int b){
if(a<b)
return a;
return b;
}
int dfs(int k,int j,int cnt){
int i;
if(j<0){
if(k==0)
mn=cnt;
return 0;
}
i=min(k/st[j].sm,s[mp[st[j].sm]]);
dfs(k-i*st[j].sm,j-1,cnt+i);
return 0;
}
int main(){
init();
int n,q;
int i,j,a;
scanf("%d%d",&n,&q);
for(i=0;i<n;i++){
scanf("%d",&a);
s[mp[a]]++;
}
while(q--){
mn=0;
scanf("%d",&a);
dfs(a,31,0);
if(mn==0)
printf("-1\n");
else
printf("%d\n",mn);
}
return 0;
}
codeforces1003D(贪心)的更多相关文章
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]
1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 786 Solved: 391[Submit][S ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【BZOJ-4245】OR-XOR 按位贪心
4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 486 Solved: 266[Submit][Sta ...
- code vs 1098 均分纸牌(贪心)
1098 均分纸牌 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有 N 堆纸牌 ...
- 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心
SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...
- 【贪心】HDU 1257
HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...
随机推荐
- 移动端 meta 必备
将页面宽度到跟手机宽度比例相同,在手机上不能用手缩放 <meta name="viewport" content="width=device-width" ...
- 2.1 uml序言
UML Unified Modeling Language 统一建模语言 模型的定义 建模 modeling 重要的研发成果常常产自类比(analogy): 把不太理解的东西和一些已经较为理解.且十分 ...
- dsu on tree练习
dsu on tree主要是处理一些有根树子树询问的操作, 作用与点分治和线段树合并类似. 一般无根树询问所有树链信息的直接就点分了, 有根树的话一般用线段树合并或dsu on tree, 线段树合并 ...
- 统计数组中各个元素出现的次数,元素取值范围为:1到N
问题描述: * 给定一个整数数组a,长度为N,元素取值范围为[1,N]. * 统计各个元素出现的次数,要求时间复杂度为O(N),空间复杂度为O(1). * 可以改变原来数组结构. 思路: * 从第 ...
- python装饰器概念与应用
格式一:装饰器外层不传参,内层传参 user_status = False # 用户登录了就把这个改成True def login(func): # 把要执行的henan模块从这里传进来 def in ...
- python中lambda的用法
一.lambda函数也叫匿名函数,即,函数没有具体的名称.先来看一个最简单例子: def f(x):return x**2print f(4) Python中使用lambda的话,写成这样 g = l ...
- Ping 的TTL理解
http://www.webkaka.com/tutorial/zhanzhang/2017/061570/ 根据自己的扩展重新整理了一下,虽然不是运维,想了解一点东西就希望了解清楚. 一.含义 “T ...
- Spring注解之BeanPostProcessor与InitializingBean
/*** BeanPostProcessor 为每个bean实例化时提供个性化的修改,做些包装等*/ package org.springframework.beans.factory.config; ...
- 有史以来性价比最高最让人感动的一次数据库&SQL优化(DB & SQL TUNING)——半小时性能提升千倍
昨天,一个客户现场人员急急忙忙打电话找我,说需要帮忙调优系统,因为经常给他们干活,所以,也就没多说什么,先了解情况,据他们说,就是他们的系统最近才出现了明显的反应迟钝问题,他们的那个系统我很了解,软硬 ...
- Http Header Content-Typ
Http Header里的Content-Type一般有这三种:application/x-www-form-urlencoded:数据被编码为名称/值对.这是标准的编码格式.multipart/fo ...