题干:

Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment.

Amr has n different types of chemicals. Each chemical i has an initial volume of ailiters. For this experiment, Amr has to mix all the chemicals together, but all the chemicals volumes must be equal first. So his task is to make all the chemicals volumes equal.

To do this, Amr can do two different kind of operations.

  • Choose some chemical i and double its current volume so the new volume will be 2ai
  • Choose some chemical i and divide its volume by two (integer division) so the new volume will be 

Suppose that each chemical is contained in a vessel of infinite volume. Now Amr wonders what is the minimum number of operations required to make all the chemicals volumes equal?

Input

The first line contains one number n (1 ≤ n ≤ 105), the number of chemicals.

The second line contains n space separated integers ai (1 ≤ ai ≤ 105), representing the initial volume of the i-th chemical in liters.

Output

Output one integer the minimum number of operations required to make all the chemicals volumes equal.

Examples

Input

3
4 8 2

Output

2

Input

3
3 5 6

Output

5

Note

In the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal 4.

In the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal 1.

题目大意:

给你n个整数,每一个整数可以进行两种操作,除2(取整)或者乘2.每个整数可以进行任意次这样的操作。

使这n个整数都变为相同的整数最少需要多少次操作。

解题报告:

bfs考虑每一个数可以到达的地方以及他可以该地方的所有的步数。注意一个细节就是每次bfs不要都清空vis,可能会超时,因为肯定memset了很多本来就是0的值,可以选择vis[i]=i来判断是否走过。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
const int INF = 0x3f3f3f3f;
int cnt[MAX], vis[MAX], steps[MAX];
int main() {int n, res = INF, x, y;scanf("%d", &n);for(int i=1; i<=n; i++) {scanf("%d", &x);queue<pair<int, int> > q;q.push(pm(x, 0));while(!q.empty()) {x = q.front().first;y = q.front().second;q.pop();if(x > 100003) continue;if(vis[x] == i) continue;vis[x] = i;steps[x]+=y;cnt[x]++;q.push(pm(x * 2, y + 1));q.push(pm(x / 2, y + 1));}}for(int i=0; i<=100000; i++)if(cnt[i] == n)if(res > steps[i])res = steps[i];printf("%d", res);return 0;
}

【CodeForces - 558C】Amr and Chemistry(位运算,bfs,计数,思维,tricks)相关推荐

  1. 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry

    C. Amr and Chemistry Problem's Link: http://codeforces.com/problemset/problem/558/C Mean: 给出n个数,让你通过 ...

  2. Codeforces 558C Amr and Chemistry 暴力 - -

    点击打开链接 Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input stan ...

  3. 【CodeForces 1042B --- Vitamins】DP+位运算

    [CodeForces 1042B --- Vitamins]DP+位运算 题目来源:点击进入[CodeForces 1042B - Vitamins] Description Berland sho ...

  4. hdu 1818 It's not a Bug, It's a Feature!(位运算+bfs优先队列)

    题意:给一个长度为n的bug,和m个补丁,然后是m个补丁的描述.第一个数字是这个补丁消耗的时间. 第1个字符串是这个补丁要工作需要满足的条件,第2个字符串是这个补丁的作用 详细一点说, 对于第一个字符 ...

  5. !codeforces 558C Amr and Chemistry-yy题-(位运算相关)

    题意:有n个数,每次进行的操作只能是除以2或者乘以2,求这n个数转换成同一个数字所需要的最小的操作步数 分析: 乍一看题目,觉得好难,对于这种每次有两种情况求最后到达的终点的balabala的我就觉得 ...

  6. jzoj100047-基因变异【位运算,bfs】

    正题 题目大意 一个长度为nnn的序列aaa. 对于一个数每秒可以将一个二进制位取反或异或aaa中的一个数. qqq个询问,询问从xxx变化到yyy最少要多少秒. 解题思路 对于一个x和yx和yx和y ...

  7. P1457 城堡 The Castle 位运算+BFS+思维(难题,好题)

    题目描述 我们憨厚的USACO主人公农夫约翰(Farmer John)以无法想象的运气,在他生日那天收到了一份特别的礼物:一张"幸运爱尔兰"(一种彩票).结果这张彩票让他获得了这次 ...

  8. USACO2.1【bfs,排序,贪心,dfs,位运算】

    前言 开始刷USACO的题了. 正题 刷前必备技能:OI常识,bfs,dfs,位运算,基础贪心,快速排序. T1:城堡 TheCastleThe CastleTheCastle 评测记录: https ...

  9. 黑白棋游戏 (BFS + 位运算初步运用)

    题目描述   黑白棋游戏的棋盘由4×4方格阵列构成.棋盘的每一方格中放有1枚棋 子,共有8枚白棋子和8枚黑棋子.这16枚棋子的每一种放置方案都构成一个游戏状态.在棋盘上拥有1条公共边的2个方格称为相邻 ...

最新文章

  1. 信号与系统:快速傅里叶变换FFT中的实际频率(奈奎斯特频率解析)
  2. JS字符串转换为JSON的四种方法笔记
  3. 接口500什么原因_80%小餐饮店几乎都“活“不过500天,为什么?都在这5个原因里...
  4. 信息学奥赛C++语言:与 7 无关的数的个数
  5. php上传文件大小读取,怎么修改php上传文件的大小限制?
  6. 把分类信息,在表格中展现出来,通过合并单元格来实现信息之间的层级关系...
  7. OSPFv3中LSA详解(三)——Network LSA变化
  8. LSTM matlab实现
  9. matlab norm函数使用_matlab中norm函数的用法(图)
  10. clodop配置SSL证书 WEB打印机服务
  11. 漫画:什么是 B+ 树?
  12. 明哥手把手《闲鱼快速入门指南》电子书!!
  13. BugkuCTF 秋名山老司机wp
  14. figcaption标签css,[HTML]正确使用Figure和Figcaption标签(译)_html/css_WEB-ITnose
  15. 埃尔米特曲线 Hermite Curve
  16. 无线网络攻防实战 WEP密钥如何被攻破的 图
  17. 基于tushare量化交易模型基础构建(偿债能力排名)
  18. 老猿学5G扫盲贴:N6接口用户平面协议栈对应的网络分层模型
  19. 02矩阵01 —— 基本矩阵 : 对角矩阵、方幂、数量矩阵、转置矩阵、对称矩阵、逆矩阵、奇异矩阵、三角矩阵
  20. 中国软件开发标准各项文档模板下载(附模版)

热门文章

  1. NetTiers模板中各个选项的一些解释
  2. 计算机应用综合实践实验心得,综合实践活动培训心得体会范文(精选5篇)
  3. linux c c 常用的日志库,mslog: 一款超轻量级的C日志库,无需依赖额外的库,测试或移植过的系统有Linux(ubuntu,centos),Windows以及部分嵌入式设备;...
  4. 弹出并点击弹框关闭 自定义toast_关于别名配置使用弹框交互应用的思考
  5. android 手机获取公网ip_KSWEB 旧手机搭建网站服务
  6. 法那科机器人初始化启动_发那科机器人的控制方式和控制柜的启动方式
  7. python数组元素复制_python的numpy数组 的复制问题?
  8. win7下卸载linux系统(不用windows光盘)
  9. sqlserver oracle插件,SQLServer链接服务器至Oracle
  10. CentOS卸载OpenJDK并安装Sun JDK