Codeforces1003D——Coins and Queries

Polycarp has n coins, the value of the i-th coin is ai. It is guaranteed that all the values are integer powers of 2 (i.e. ai=2d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number bj. The answer to the query is the minimum number of coins that is necessary to obtain the value bj using some subset of coins (Polycarp can use only coins he has). If Polycarp can’t obtain the value bj, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn’t affect Polycarp’s coins).
Input
The first line of the input contains two integers n and q (1≤n,q≤2⋅105) — the number of coins and the number of queries.
The second line of the input contains n integers a1,a2,…,an — values of coins (1≤ai≤2⋅109). It is guaranteed that all ai are integer powers of 2 (i.e. ai=2d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer bj — the value of the j-th query (1≤bj≤109).
Output
Print q integers ansj. The j-th integer must be equal to the answer on the j-th query. If Polycarp can’t obtain the value bj the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2

不知道为什么是贪心,反正贪心就没错了
读取的时候记录下最大的硬币面值,同时把每个面值加到map里面,方便后面查找
然后循环就从最大面值开始,依次除2,如果没有该面值,也就是map值为0,则跳过
int use=min(res[i],k/i); 然后注意这一句,要找出手上有的硬币个数和应该拿的硬币数的最小值,比如12/4=3需要3个4元硬币,但实际res[4]=2,只有两个,则要取小的
然后就判断什么时候凑齐和什么时候无法凑齐

代码:

#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
map<int,int> res;
int main(void){
    int n,q,t;
    int k;
    scanf("%d%d",&n,&q);
    int Max=0;
    while(n--){
        scanf("%d",&t);
        Max=max(Max,t);
        res[t]++;
    }
    while(q--){
        int ans=0;
        scanf("%d",&k);
        for(int i=Max;i>0;i/=2){
            if(res[i]==0){
                continue;
            }
            else{
                int use=min(res[i],k/i);
                //printf("%d %d %d\n",k,i,use);
                ans+=use;
                k-=use*i;
            }

            if(k==0){
                break;
            }
        }
        if(k!=0){
            printf("-1\n");
        }
        else{
            printf("%d\n",ans);
        }
    }
    return 0;
}
全部评论

相关推荐

02-07 12:06
已编辑
华侨大学 测试开发
最近看到很多&nbsp;92&nbsp;的,甚至是硕士,开始往测开赛道卷,说实话有点看不懂。先把话说清楚,大厂里的测开,绝大多数时间干的还是测试的活,只是写点自动化脚本、维护测试平台、接接流水线,真正像开发一样做系统、做架构、做核心平台的测开少得可怜,基本都集中在核心提效组,而且人很少,外面进去的大概率轮不到你,我想真正干过人都清楚。很多人被洗脑了,以为测开也是开,和后端差不多,只是更简单、更轻松、还高薪。现实情况是,测开和开发的职业路径完全不一样。开发的核心是业务和系统能力,测开的核心是稳定性和覆盖率,前者是往上走,后者天花板非常明显。你可以见到很多开发转测开,但你很少见到干了几年测开还能顺利转回开发的。更现实一点说,92&nbsp;的高学历如果拿来做测开,大部分时间就是在做重复性很强的杂活,这种工作对个人能力的放大效应非常弱。三年下来,你和一个双非的,甚至本科的测开差距不会太大,但你和同龄的后端、平台开发差距会非常明显。这不是努不努力的问题,是赛道问题。所谓测开简单高薪,本质上是把极少数核心测开的上限,当成了整个岗位的常态来宣传。那些工资高、技术强的测开,本身就是开发水平,只是挂了个测开的名。普通人进去,99%&nbsp;做的都是项目兜底型工作,而不是你想象中的平台开发。测开不是不能做,但它绝对不是开发的平替,也不是性价比最优解。如果你是真的不想做开发,追求稳定,那测开没问题。但如果你只是觉得测开比后端容易,还能进大厂,那我劝你冷静一点,这只是在用短期安全感换长期天花板。有92的学历,如果你连测开这些重复性工作都能心甘情愿接受,那你把时间精力用在真正的开发、系统、业务深度上,回报大概率比卷测开要高得多。想清楚再下场,别被岗位名和话术带偏了,就算去个前端客户端也是随便占坑的,测开是一个坑位很少赛道,反而大面积学历下放,不用想也能知道会是什么结果,我想各位在JAVA那里已经看到了
小浪_Coding:工作只是谋生的手段 而不是相互比较和歧视
点赞 评论 收藏
分享
艾莉Alliy:拼多多好像很看重笔试,我秋招的时候笔试ak了,面试问了好多八股,全都说“不会”,照样全部通过了面试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务