CodeForces-1213D Equalizing by Division

Description

You are given an array a consisting of n integers. In one move you can choose any aiai and divide it by 2 rounding down (in other words, in one move you can set \(a_i=\lfloor \frac{a_i}{2} \rfloor\)).

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≤2⋅1051≤k≤n≤2⋅105) — 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

题解

直接暴力分解。

排序一遍之后统计每个数出现了多少次,出现k次的时候更新一遍答案即可,打div3的时候硬是没想到,基础过差

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 50;
int cnt[N];
int num[N];
int a[N];
int main() {
    int n, k;
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    sort(a + 1, a + n + 1);
    int res = 1e9;
    for (int i = 1; i <= n; i++) {
        int tmp = 0;
        while (a[i]) {
            cnt[a[i]]++;
            num[a[i]] += tmp;
            if (cnt[a[i]] == k) {
                res = min(res, num[a[i]]);
            }
            tmp++; a[i] /= 2;
        }
    }
    printf("%d\n", res);
    return 0;
}

转载于:https://www.cnblogs.com/artoriax/p/11456111.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值