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:=⌊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
5 3
1 2 2 4 5
1
5 3
1 2 3 4 5
2
5 3
1 2 3 3 3
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的更多相关文章
- D2. Equalizing by Division (hard version)
D2. Equalizing by Division (hard version) 涉及下标运算一定要注意下标是否越界!!! 思路,暴力判断以每个数字为到达态最小花费 #include<bits ...
- Codeforces 1213D Equalizing by Division
cf题面 中文题意 给n个数,每次可以把其中一个数字位运算右移一位(即整除以二),问要至少操作几次才能让这n个数中有至少k个相等. 解题思路 这题还有个数据范围更小的简单版本,n和k是50,\(a_i ...
- CF1213D Equalizing by Division
easy version hard version 问题分析 直接从hard version入手.不难发现从一个数\(x\)能得到的数个数是\(O(\log x)\)的.这样总共有\(O(n\log ...
- codeforces Equalizing by Division (easy version)
output standard output The only difference between easy and hard versions is the number of elements ...
- Codeforces Round 582
Codeforces Round 582 这次比赛看着是Div.3就打了,没想到还是被虐了,并再次orz各位AK的大神-- A. Chips Moving 签到题.(然而签到题我还调了20min--) ...
- CF 题目选做
写省选的题目对noip没什么大用 关键是 细节题或者是思考题比较重要 练思维自然是CF比较好了 把我见到的比较好的CF题放上来刷一刷. LINK:Complete the projects 就是说一个 ...
- python from __future__ import division
1.在python2 中导入未来的支持的语言特征中division(精确除法),即from __future__ import division ,当我们在程序中没有导入该特征时,"/&qu ...
- [LeetCode] Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- 关于分工的思考 (Thoughts on Division of Labor)
Did you ever have the feeling that adding people doesn't help in software development? Did you ever ...
随机推荐
- 第四周java实验
实验四 类与对象的定义及使用 实验时间 2018-9-20 1.实验目的与要求 (1) 理解用户自定义类的定义: 类是具有相同属性和行为的一组对象的集合.java中,用构造器构造并初始化对象. 类是构 ...
- CF1326A Bad Ugly Numbers 题解
原题链接 简要题意: 构造一个长为 \(n\) 的数,使得每位均不为 \(0\),且 \(n\) 不被它的各位数字整除. 比方说, \(n = 239\) 是合法的.因为: \(2 \not | 23 ...
- Android 文本TextView底部对齐实现
前言 想要实现主体文字突出显示,前面是数值加粗,后面是单位符号,且底部对齐数值显示的效果:但是不同TextView字体大小排版后总是有些差别,无法底部对齐.百度一番后有重写TextView的,还有其它 ...
- JSON字符串带BOM头"ufeff"
调用三方接口返回值JSON字符串带BOM头"\ufeff",JSON解析死活报错. 我是用SpringBoot的RestTemplate调用三方接口的,一开始返回值我是用对象接收返 ...
- 记录一些服务端术语和搭建web服务器
菜单快捷导航 服务端常用术语 搭建web服务器和配置虚拟主机 记录一些服务端方面的常用术语 1.CS架构和BS架构 1.1 CS架构 CS(Client/Server),基于安装包类型的桌面或手机软件 ...
- 【笔记3-31】Python语言基础-字典dict
创建字典 dict1 = {'k1': 'v1', 'k2': 'v2', 'k3': 'v3', 'k4': 'v4'} dict2 = dict(k1='v1', k2='v2', k3='v3' ...
- 如何将一篇文章导入Endnote并将引用插入Word
Endnote作为一款专注管理文献引用的工具用起来还是很方便的,极大地简化了管理引用格式等相关工作,让我们能够把更多精力用在写文章本身. 今天就介绍一下如何将一篇我们看到的觉得有参考价值的文章导入wo ...
- Java基础语法(8)-数组中的常见排序算法
title: Java基础语法(8)-数组中的常见排序算法 blog: CSDN data: Java学习路线及视频 1.基本概念 排序: 是计算机程序设计中的一项重要操作,其功能是指一个数据元素集合 ...
- TensorFlow系列专题(十三): CNN最全原理剖析(续)
目录: 前言 卷积层(余下部分) 卷积的基本结构 卷积层 什么是卷积 滑动步长和零填充 池化层 卷积神经网络的基本结构 总结 参考文献 一.前言 上一篇我们一直说到了CNN[1]卷积层的特性,今天 ...
- G - 土耳其冰淇凌 Gym - 101194D(二分答案 + 贪心检验)
熊猫先生非常喜欢冰淇淋,尤其是冰淇淋塔.一个冰淇淋塔由K个冰淇淋球堆叠成一个塔.为了使塔稳定,下面的冰淇淋球至少要有它上面的两倍大.换句话说,如果冰淇淋球从上到下的尺寸是A0, A1, A2,···, ...