牛客挑战赛45(除2!)[思维+贪心]

104 篇文章 0 订阅
52 篇文章 1 订阅

给一个数组,一共有n个数。
你能进行最多 k次操作。每次操作可以进行以下步骤:

  • 选择数组中的一个偶数 ai​,将其变成 ai​/2 。

现在你进行不超过k次操作后,让数组中所有数之和尽可能小。请输出这个最小的和。


思路:贪心即可,先把偶数的存下来,然后每次挑出最大的偶数/2,如果除了后还是偶数,放回去,不然就累加贡献,不放回去。每次拿最大的直接priority_que即可。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef long long LL;
priority_queue<LL>que;
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,k;cin>>n>>k;
  LL sum=0;
  for(LL i=1;i<=n;i++){
  	LL x;cin>>x;
  	if(x&1) sum+=x;
  	else que.push(x);
  }
  ///debug(sum);
  LL cnt=0;
  while(!que.empty())
  {
  	cnt++;
  	if(cnt>k) break;
	LL t=que.top();que.pop();
  	t/=2;
  	if(t&1) sum+=t;
  	else que.push(t);
  }
  while(!que.empty()){
  	LL t=que.top();
 /// 	debug(t);
	sum+=t;
  	que.pop();
  }
  cout<<sum<<endl;
return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值