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

Equalizing by Division的更多相关文章

  1. D2. Equalizing by Division (hard version)

    D2. Equalizing by Division (hard version) 涉及下标运算一定要注意下标是否越界!!! 思路,暴力判断以每个数字为到达态最小花费 #include<bits ...

  2. Codeforces 1213D Equalizing by Division

    cf题面 中文题意 给n个数,每次可以把其中一个数字位运算右移一位(即整除以二),问要至少操作几次才能让这n个数中有至少k个相等. 解题思路 这题还有个数据范围更小的简单版本,n和k是50,\(a_i ...

  3. CF1213D Equalizing by Division

    easy version hard version 问题分析 直接从hard version入手.不难发现从一个数\(x\)能得到的数个数是\(O(\log x)\)的.这样总共有\(O(n\log ...

  4. codeforces Equalizing by Division (easy version)

    output standard output The only difference between easy and hard versions is the number of elements ...

  5. Codeforces Round 582

    Codeforces Round 582 这次比赛看着是Div.3就打了,没想到还是被虐了,并再次orz各位AK的大神-- A. Chips Moving 签到题.(然而签到题我还调了20min--) ...

  6. CF 题目选做

    写省选的题目对noip没什么大用 关键是 细节题或者是思考题比较重要 练思维自然是CF比较好了 把我见到的比较好的CF题放上来刷一刷. LINK:Complete the projects 就是说一个 ...

  7. python from __future__ import division

    1.在python2 中导入未来的支持的语言特征中division(精确除法),即from __future__ import division ,当我们在程序中没有导入该特征时,"/&qu ...

  8. [LeetCode] Evaluate Division 求除法表达式的值

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  9. 关于分工的思考 (Thoughts on Division of Labor)

    Did you ever have the feeling that adding people doesn't help in software development? Did you ever ...

随机推荐

  1. Python 趣题

    如何优雅判断list为空 list_temp = [] if list_temp: # 存在值即为真 else: # list_temp是空的 在Python中,False,0,'',[],{},() ...

  2. Longest subarray of target sum

    2018-07-08 13:24:31 一.525. Contiguous Array 问题描述: 问题求解: 我们都知道对于subarray的问题,暴力求解的时间复杂度为O(n ^ 2),问题规模已 ...

  3. MySQL优化之避免索引失效的方法

    在上一篇文章中,通过分析执行计划的字段说明,大体说了一下索引优化过程中的一些注意点,那么如何才能避免索引失效呢?本篇文章将来讨论这个问题. 避免索引失效的常见方法 1.对于复合索引的使用,应按照索引建 ...

  4. Building Applications with Force.com and VisualForce(Dev401)(十五):Data Management: Data management Overview

    Dev401-016:Data Management: Data management Overview Course Objectives1.List typical data management ...

  5. maven resource filter 说明和配置方法

    <maven> <dependencies> <dependency> ... </dependency> </dependencies> ...

  6. ArcGIS Desktop的安装

    1.双击ArcGIS Desktop安装目录下的Setup.exe. 2.点击“下一步”. 3.选择“我接受许可协议(A)”,点击“下一步”. 4.选择“完全安装”,点击“下一步”. 5.点击“更改” ...

  7. openssl与java(读取加密码的密钥)

    最近在研究java security方面的东西,java自带可以使用各种加密解密算法(rsa,dsa,des3,aes,md5...),但往往一些密钥是第三方给我们的,我们需要用java装载这些密钥然 ...

  8. 前端经典面试题解密:JS的new关键字都干了什么?

    前言 new关键字在实例化获取对象时都做了什么?是一道经常出现在前端面试时的问题.如果只是简单的了解new关键字是实例化构造函数获取对象,是万万不能够的.更深入的层级发生了什么呢?同时面试官想从这道题 ...

  9. list容器排除重复单词的程序

    #include<iostream> #include<fstream> #include<string> #include<algorithm> #i ...

  10. Blazor入门笔记(2)-分部类组件与组件的继承

    1.前言 本文接自Blazor的组件(1)-从0构建一个组件 2.分部类组件 Razor组件你可理解为就是一个类名与文件名相同的类,因此,可以新建一个同名的partial类,将组件中@code里面的代 ...