CF1213D Equalizing by Division
问题分析
直接从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的更多相关文章
- 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 ... 
- Equalizing by Division
		The only difference between easy and hard versions is the number of elements in the array. You are g ... 
- 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 ... 
随机推荐
- 面试35-删除字符串重复字符-删除出现在第二个字符串中的字符-第一个只出现一次的字符-hash表计数
			#include<iostream>#include<algorithm>#include<functional>using namespace std;char ... 
- HDU 2018 Cow Story DP
			Basic DP Problem URL:https://vjudge.net/problem/HDU-2018 Describe: There is a cow that gives birth t ... 
- python基本数据类型零碎知识点
			... 
- ado.net SqlHelp类
			using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ... 
- EJS学习(三)之语法规则中
			⚠️实例均结合node,也就是AMD规范版本 ejs中使用render()表示渲染文本 接收三个参数:模版字符串.data.options,返回一个字符串 const ejs = require('e ... 
- js 禁用F12 和右键查看源码
			<script> window.onkeydown = function(e) { if (e.keyCode === 123) { e.preventDefault() } } wind ... 
- js 数据类型的判断
			1. typeof 运算符 typeof 可以判断基本数据类型: typeof 123; // "number" typeof 'abc'; // "string&quo ... 
- 黑马java课程视频java学习视频
			资料获取方式,关注公总号RaoRao1994,查看往期精彩-所有文章,即可获取资源下载链接 更多资源获取,请关注公总号RaoRao1994 
- Layedit 编辑页面赋值
			1.编辑页面 $("[name=Experience]").val(data.Experience);//直接赋值然后再进行build experience = layedit.b ... 
- Delphi Label组件
