2021湖北省赛 签到题

F. Battery
比较容易假贪心ac,数据有点水
看 序列 b 可以猜出是道位运算的题目
把每个数都拆成二进制的和的形式存起来
然后再贪心

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 9;

priority_queue <int> q;
int n, m, a[31], b[31], c[N], ans, x;

int main()
{
	cin >> n >> m;
	for(int i = 1; i <= n; ++i)
	{
		scanf("%d", &x);
		for(int j = 0; j <= 30; ++j)
			if(x & (1 << j)) a[j]++;
	}	
	for(int i = 1; i <= m; ++i)
	{
		scanf("%d", &x);
		for(int j = 0; j <= 30; ++j)
			if(x & (1 << j)) b[j]++;
	}
	//for(int j = 0;j <= 30; ++j) printf("%d ",a[j]); puts("");
	//for(int j = 0;j <= 30; ++j) printf("%d ",b[j]); puts("");
	for(int i = 0; i <= 30; ++i)
	{
		ll tmp = (1ll*a[i]) << i; 
		for(int j = 0; j <= i; ++j)
		{// j <= i 保证了贪心的正确性
			if(tmp >= b[j]) ans += b[j], tmp -= b[j], b[j] = 0;
			else {
				ans += tmp; b[j] -= tmp; break;
			}
			tmp >>= 1;
		}
	}
	cout << ans << endl;
	return 0;
}

然鹅上边代码中 tmp>>=1 这句话不是太能理解为啥可以
就自己根据理解改了一下也能ac
不开ll见祖宗,草

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 9;

priority_queue <int> q;
ll n, m, a[31], b[31], c[N], ans, x;

int main()
{
	cin >> n >> m;
	for(int i = 1; i <= n; ++i)
	{
		scanf("%d", &x);
		for(int j = 0; j <= 30; ++j)
			if(x & (1 << j)) a[j]++;
	}	
	for(int i = 1; i <= m; ++i)
	{
		scanf("%d", &x);
		for(int j = 0; j <= 30; ++j)
			if(x & (1 << j)) b[j]++;
	}
	for(int i = 0; i <= 30; ++i)
	{
		ll tmp = (1ll*a[i]) << i;
		for(int j = 0; j <= i; ++j)
		{
			if(tmp >= (b[j] << j))
			{
				ans += b[j], tmp -= (b[j] << j), b[j] = 0;
			}
			else {
				ll x = 1ll << j;
				ans += tmp / x; b[j] -= tmp / x; 
				break;
			}
		}
	}
	cout << ans << endl;
	return 0;
}
/*
4 4
3 5 3 3
1 1 2 4
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值