easy version

hard version

问题分析

直接从hard version入手。不难发现从一个数\(x\)能得到的数个数是\(O(\log x)\)的。这样总共有\(O(n\log n)\)个数。然后对每一种数开一个大根堆维护前\(k\)个就好了。

参考程序

#include <bits/stdc++.h>
using namespace std; const int INF = 2147483647;
const int Maxn = 200010;
const int MaxAlpha = 200000;
int n, k, A[ Maxn ], Sum[ Maxn ];
priority_queue< int > Pq[ Maxn ];
int main() {
scanf( "%d%d", &n, &k );
for( int i = 1; i <= n; ++i ) scanf( "%d", &A[ i ] );
sort( A + 1, A + n + 1 );
for( int i = 1; i <= n; ++i ) {
int t = 0;
if( Pq[ A[ i ] ].size() == k ) Sum[ A[ i ] ] -= Pq[ A[ i ] ].top(), Pq[ A[ i ] ].pop();
Pq[ A[ i ] ].push( 0 );
while( A[ i ] ) {
++t; A[ i ] /= 2;
if( Pq[ A[ i ] ].size() < k ) Pq[ A[ i ] ].push( t ), Sum[ A[ i ] ] += t;
else
if( Pq[ A[ i ] ].top() > t ) {
Sum[ A[ i ] ] -= Pq[ A[ i ] ].top(), Pq[ A[ i ] ].pop();
Sum[ A[ i ] ] += t; Pq[ A[ i ] ].push( t );
}
}
}
int Ans = INF;
for( int i= 0; i <= MaxAlpha; ++i )
if( Pq[ i ].size() == k )
Ans = min( Ans, Sum[ i ] );
printf( "%d\n", Ans );
return 0;
}

CF1213D 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. Equalizing by Division

    The only difference between easy and hard versions is the number of elements in the array. You are g ...

  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. 设计模式在 Spring 框架中的良好应用

    在开始正文之前,请你先思考几个问题: 你项目中有使用哪些 GOF 设计模式 说一说 GOF 23 种设计模式的设计理念 说说 Spring 框架中如何实现设计模式 假设我是面试官问起了你这些面试题,你 ...

  2. python中super函数的参考

    https://rhettinger.wordpress.com/2011/05/26/super-considered-super/ http://wiki.jikexueyuan.com/proj ...

  3. win10操作系统的安装

    电脑被重装操作系统了,一切从头开始啦!!! 不过倒是学习了,给大家分享一些学习经验~ 1:制作启动盘 制作启动盘的首先要准备一个空的U盘,为什么说空的呢,因为制作的时候会格式化U盘,只能存个操作系统, ...

  4. springboot(十九)-线程池的使用

    我们常用ThreadPoolExecutor提供的线程池服务,springboot框架提供了@Async注解,帮助我们更方便的将业务逻辑提交到线程池中异步执行. 话不多说,编码开始: 1.创建spri ...

  5. openlayers之全屏控件的使用

    import { FullScreen } from 'ol/control' map.addControl(new FullScreen())

  6. python3.7 利用pyhive 连接上hive(亲测可用)

    来python爬虫中,经常会遇到数据的存储问题,如果有大量数据,hive存储是个不错的选择. 那么python如何来连接hive呢?网上有各种教程但是都不是很好用,亲自测试pyhive可用 要求:可用 ...

  7. 关于IDEA,多服务运行 Services -> Run Dashboard 部分服务添加变灰色,限制使用5个启动类,重启之后需要重新添加,服务在 Run Dashboard 中的显示排序问题,不显示 Services(Run Dashboard)

    我的IDEA版本为最新版本 变灰色的原因就是因为右键删除了那个启动的主配置类,然后就会显示灰色,再次打开这个醒目,就不会在Run Dashboard中显示这个主配置类了 解决方法 如果你要 调整这些服 ...

  8. linux yum 安装及卸载

    在Centos中yum安装和卸载软件的使用方法安装方法安装一个软件时yum -y install httpd安装多个相类似的软件时yum -y install httpd*安装多个非类似软件时yum ...

  9. zabbix 性能优化

    Zabbix 安装好就放在那不管了,以为不需要调优.直到最近出现了如下一堆告警. 描述下我们的环境 硬件:8核 32G 软件:Centos7.6 Zabbix4.0.Httpd2.4.PHP7.3.M ...

  10. linux发行版及版本号

    1991年8月:Linus Torvalds宣布成立Linux            遵行GPL: Kernel:底层监控程序又叫通用程序,即我们所说的操作系统    Kernel的作用:       ...