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;
}

转载于:https://www.cnblogs.com/pubgoso/p/10759723.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值