Equalizing by Division

Posted accepting

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Equalizing by Division相关的知识,希望对你有一定的参考价值。

 

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:=ai2ai:=⌊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 (1kn21051≤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 (1ai21051≤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
题目大意:数组中有n个元素,每次操作可以使一个元素变为其1/2,问最少执行多少步,才能使数组中有k个相等的元素
题解:首先我们要排一下序,从小到大排,然后对每个,枚举每个元素,枚举的同时除二,同时用一个数组cnt记录在除二的过程的出现的数字,并记录一下,在用一个数组num记录arr[i]变成当前状态需要几步。
#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,m;
    cin>>n>>m;
    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 x=arr[i];
        int temp=0;
        while(x)
            cnt[x]++;
            num[x]+=temp;
            if(cnt[x]==m) 
                ans=min(ans,num[x]);
            
            temp++;
            x/=2;
        
    
    cout<<ans<<endl;
    
    return 0;

 



以上是关于Equalizing by Division的主要内容,如果未能解决你的问题,请参考以下文章

2019年个人训练赛第一场-E - Equalizing by Division (hard version)

#598 (Div. 3) F.Equalizing Two Strings

Equalizing Two Strings

Equalizing Two Strings CodeForces - 1256F (思维)

Codeforces Round #598 (Div. 3) F Equalizing Two Strings(构造题)

查询方法 find...By、read...By、query...By 和 get...By 在 spring 数据中有啥区别?