Equalizing by Division (easy version)

题目描述:

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.

大致题意:

给出一个数组arr,可以将数组里的数进行向下取整的除2操作,求最少的操作数使得数组中有k个数相同

解题思路:

由于数据范围比较小,可以将数组的所有除2的数全部存在vector里面,然后求出arr数组中每个数到vector数的操作数

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int N=2E5+7;
const int inf=1e9+7;
int arr[N];
int num[N];
int cnt[N];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=1; i<=n; i++) scanf("%d",&arr[i]);
    sort(arr+1,arr+1+n);
    int ans=inf;
    for(int i=1; i<=n; i++)
    {
        int temp=0;
        int x=arr[i];
        while(x)
        {
            cnt[x]++;
            num[x]+=temp;
            if(cnt[x]==k)
                ans=min(ans,num[x]);
            temp++;
            x/=2;
        }
    }
    printf("%d",ans);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值