Equalizing by Division (easy version) CodeForces - 1213D1(思维)

The only difference between easy and hard versions is the number of elements in the array.

You are given an array aa consisting of nn integers. In one move you can choose any aiai and divide it by 22 rounding down (in other words, in one move you can set ai:=⌊ai2⌋ai:=⌊ai2⌋).

You can perform such an operation any (possibly, zero) number of times with any aiai.

Your task is to calculate the minimum possible number of operations required to obtain at least kk equal numbers in the array.

Don’t forget that it is possible to have ai=0ai=0 after some operations, thus the answer always exists.

Input
The first line of the input contains two integers nn and kk (1≤k≤n≤501≤k≤n≤50) — the number of elements in the array and the number of equal numbers required.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105), where aiai is the ii-th element of aa.

Output
Print one integer — the minimum possible number of operations required to obtain at least kk equal numbers in the array.

Examples
Input
5 3
1 2 2 4 5
Output
1
Input
5 3
1 2 3 4 5
Output
2
Input
5 3
1 2 3 3 3
Output
0
题意:一次操作是数组中的某个元素除以2下取整。问最少操作多少次可以使这个数组中有k个相等的数字。
思路:一开始看到50的样例,就直接暴力的,可是暴力都没过,嘤嘤嘤。
我们用num[x]记录x出现过的次数,cnt[x]记录到达现在的x数量,需要操作的次数。不断的更新最小值。
代码如下:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int maxx=2e5+100;
int cnt[maxx];
int num[maxx];
int a[maxx];
int n,k;

int main()
{
	scanf("%d%d",&n,&k);
	memset(cnt,0,sizeof(cnt));
	memset(num,0,sizeof(num));
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		num[a[i]]++;
	}
	sort(a+1,a+1+n);
	int ans=inf;
	for(int i=1;i<=n;i++)
	{
		int x=a[i];
		int cnt1=0;
		while(x)
		{
			if(num[x]>=k) ans=min(ans,cnt[x]);//这里不能写==而是写>=,因为在一开始你就记录了数量有可能一开始就大于k了。
			x>>=1;
			cnt1++;
			num[x]++;//出现次数加一
			cnt[x]+=cnt1;//由a[i]转到x经过了cnt1步。
		}
	}
	cout<<ans<<endl;
}

努力加油a啊,(o)/~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值