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. .NET Core使用NPOI导出复杂,美观的Excel详解

    前言: 这段时间一直专注于数据报表的开发,当然涉及到相关报表的开发数据导出肯定是一个不可避免的问题啦.客户要求要导出优雅,美观的Excel文档格式的来展示数据,当时的第一想法就是使用NPOI开源库来做 ...

  2. 搭建Hadoop集群需要注意的问题:

    搭建Hadoop集群需要注意的问题: 1.检查三台主机名是否正确 2.检查三台IP是否正确 3.检查 /etc/hosts 映射是否正确 4.检查 JDK和Hadoop 是否安装成功(看环境变量配置) ...

  3. [UWP]使用AlphaMaskEffect提升故障艺术动画的性能(顺便介绍怎么使用性能探测器分析UWP程序)

    前几天发布了抄抄<CSS 故障艺术>的动画这篇文章,在这篇文章里介绍了如何使用Win2D绘制文字然后配合BlendEffect制作故障艺术的动画.本来打算就这样收手不玩这个动画了,但后来又 ...

  4. Nginx做负载均衡的几种轮询策略

    集群环境为了解决单点无法支撑高并发的情况,集群采用多台服务器提供服务,一般在集群中使用nginx 将来自客户端的请求转发给服务器端 nginx负载均衡可用提高网站的吞吐量,缓解单台服务器的压力. 一. ...

  5. pd库dataframe基本操作

    一.查看数据(查看对象的方法对于Series来说同样适用) 1.查看DataFrame前xx行或后xx行 a=DataFrame(data); a.head(6)表示显示前6行数据,若head()中不 ...

  6. JavaScript超越了Java,c,python等等成为Stack Overflow上最热门的

    JavaScript超越了Java,c,python等等成为Stack Overflow上最热门的标签 在2015年6月至今,JavaScript超越了Java,c,python等等成为Stack O ...

  7. 使用PyTorch进行情侣幸福度测试指南

    欢迎关注磐创博客资源汇总站: http://docs.panchuang.net/ 欢迎关注PyTorch官方中文教程站: http://pytorch.panchuang.net/ 计算机视觉–图像 ...

  8. 编译原理:DFA最小化,语法分析初步

    1.将DFA最小化:教材P65 第9题   解析: 2.构造以下文法相应的最小的DFA S→ 0A|1B A→ 1S|1 B→0S|0 解析: S→ 0A|1B →S → 0(1S|1)|1(0S|0 ...

  9. iOS 预渲染加速图像显示

    使用 UITableView 时,发现滚动时的性能还不错,但来回滚动时,第一次显示的图像不如再次显示的图像流畅,出现前会有稍许的停顿感. 于是猜想显示过的图像肯定是被缓存起来了,查了下文档后发现果然如 ...

  10. [noip模拟]水灾<BFS>

    水灾(sliker.cpp/c/pas) 1000MS  64MB 大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没. CCY ...