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. jquery的offset().top与javascript的offsetTop区别?

    offset().top是jquery的方法,需引入jquery,它获取你绑定元素上边框相对于html上边界的偏移量 offsetTop是原生js的方法,它获取你绑定元素上边框相对于离自己最近且pos ...

  2. 大数据学习(1)-shell脚本注意事项

    1.变量=值 (例如STR=abc)  不用加引号,但此时空格不再是空格字符,特殊字符可用于转义 2.等号两侧不能有空格 3.变量名称一般习惯为大写 4.双引号和单引号有区别,双引号仅将空格脱意,单引 ...

  3. spring boot JPA 数据库连接池释放

    当JPA获取数据库数据连接时,如果连接数超过最大连接数的配置,系统就会报错: Unable to acquire JDBC Connection 和: Caused by: java.sql.SQLT ...

  4. pytorch中torch.narrow()函数

    torch.narrow(input, dim, start, length) → Tensor Returns a new tensor that is a narrowed version of  ...

  5. js截取字符串相关的知识点

    截取字符串中的数字 1.使用parseInt() var str ="4500元"; var num = parseInt(str); console.log(num);//450 ...

  6. Nginx的快速安装

    1. 准备工作 1. CenterOS7.x.vmware虚拟机,安装过程参考 https://jingyan.baidu.com/article/eae0782787b4c01fec548535.h ...

  7. Postman简单的接口测试

    DownloadPostmanApphttps://www.getpostman.com/downloads/ https://www.getpostman.com/downloads/canary ...

  8. 自己实现JDK动态代理

    实验的目录结构 1.JDK动态代理 先来一段jdk动态代理的demo,首先创建一个接口,Person public interface Person { public void eat(); } 实现 ...

  9. 创建Django项目最先做的三件事情(配置文件)

    1.Templates(存放HTML文件的配置)                      <--告诉Django去哪儿找我的HTML文件 2.静态文件(CSS/JS/图片) #静态文件保存目录 ...

  10. 记一次启动Tomcat 控制台以及log4j 乱码问题

    Tomcat启动乱码 问题描述:当你发现你的Tomcat启动时乱码了,而你只是换了个Tomcat版本而已. 在找到真正的问题之前,我在网上百度了N多的资料,都试过了,但是都不行.1.修改了 windo ...