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

题干:

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;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440679.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

axure 转换为html,AxureRP教程AxureRP如何生成HTML文件

1.正常打开一份已经设计好的RP&#xff0c;如下截图所示&#xff1a;2.点击上方菜单的“发布”按钮&#xff0c;在弹出的选项中单击“生成HTML文件”&#xff0c;详细操作如下图标红位置。3.在弹出的新窗口中&#xff0c;可以设定HTML文件保存的文件夹位置&#xff0c;这个位置…

【牛客 - 373B】666RPG(线性计数dp)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/373/B 来源&#xff1a;牛客网 在欧美&#xff0c;“666”是个令人极其厌恶和忌讳的数&#xff0c;被称为“野兽数”。 相传&#xff0c;尼禄&#xff0c;这位历史上以暴君著称的古罗马皇帝&#xff0…

我家云刷android系统教程,我家云刷机教程——小白详细版(篇二)

#大男孩的快乐#征稿活动火热进行中。只要投稿就有50金币等你拿&#xff0c;更有三千元乐高大奖与达人Z计划专属权益等待优秀的你~>活动详情戳这里<前两天发了一篇我家云的刷机教程&#xff0c;没想到大家这么有兴趣&#xff0c;讨论的异常激烈。看了大家的评论才发现之前…

【Loj - 515】贪心只能过样例(暴力,或01背包 + bitset)

题干&#xff1a; 题目描述 输入格式 第一行一个数 n。 然后 n 行&#xff0c;每行两个数表示 ai​,bi​。 输出格式 输出一行一个数表示答案。 样例 样例输入 5 1 2 2 3 3 4 4 5 5 6 样例输出 26 数据范围与提示 解题报告&#xff1a; 注意到要求统计种类数&#xf…

html5文件域的自动获取,HTML5 文件域+FileReader 读取文件(一)

在HTML5以前&#xff0c;HTML的文件上传域的功能具有很大的局限性&#xff0c;这种局限性主要体现在如下两点&#xff1a;每次只能选择一个文件进行上传客户端代码只能获取被上传文件的文件路径&#xff0c;无法访问实际的文件内容一、FileList对象和File对象HTML5为typefile 的…

计算几何 模板

计算几何模板&#xff1a; #include<iostream> #include<algorithm> #include<queue> #include<cstdio> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #d…

html中如何让三个方块并排,html – 并排设置两个div,然后设置第三个div

我如何并排设置两个div,而下面的第三个div设置为这样.我当前的代码如下所示,将div放在name div之后Name6:30 PMNoteCSS&#xff1a;#contact_table_data {width:inherit;height:inherit;background-color:#99cc33;max-width:400px;}#info_div_name {width:auto;height:auto;pad…

【ZOJ - 4019】Schrödinger's Knapsack (dp,背包,贪心,组内贪心组间dp)

题干&#xff1a; 有两种物品&#xff0c;k分别为k1&#xff0c;k2&#xff0c;有大小各不一的这两种物品若干&#xff0c;放入容量为c的背包中&#xff0c;能获得求最大的值。放的顺序会影响结果。每次放入一物品&#xff0c;其获得的值都可以用vkr计算&#xff0c;r表示放入…

html 闪烁字,HTML最简单的文字闪烁代码

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼Titlekeyframes blink{0%{opacity: 1;}50%{opacity: 1;}50.01%{opacity: 0;}100%{opacity: 0;}}-webkit-keyframes blink {0% { opacity: 1; }50% { opacity: 1; }50.01% { opacity: 0; }100% { opacity: 0; }}-moz-keyframes blin…

【ZOJ - 4020 】Traffic Light (bfs,分层图)

题干&#xff1a; n*m矩阵a.若a[i][j]1则可以往左右走,若a[i][j]0 则可以往上下走. 每一秒可以按上述规则移动,并且每秒钟矩阵所有的值翻转。 n*m<1e5.问从(sx,sy)到(tx,ty)的最短时间. 解题报告&#xff1a; 这题因为不带权值所以不需要考虑Dijkstra&#xff0c;可以根据…

在计算机应用中mis,在计算机的应用中,“MIS”表示

阅读下列材料&#xff0c;回答有关问题&#xff1a;地理信息系统(GIS)&#xff0c;也称作地理资讯系统&#xff0c;是一门综合性学科&#xff0c;已经广泛地应用在不同的领域&#xff0c;是用于输入、存储、查询、分析和显示地理数据的计算机系统。GIS属于信息系统的一类&#…

【牛客 - 369C】小A与欧拉路(bfs树的直径)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/369/C 来源&#xff1a;牛客网 小A给你了一棵树&#xff0c;对于这棵树上的每一条边&#xff0c;你都可以将它复制任意&#xff08;可以为0&#xff09;次&#xff08;即在这条边连接的两个点之间再…

html loader的作用,webpack认识loader的作用

举例&#xff1a;如果希望在.html文件中使用style.css样式&#xff0c;我们以前只学习过一种方式&#xff1a;直接在.html中通过link的方式来引入 &#xff0c;这是传统的做法&#xff0c;在webpack语境下&#xff0c;我们将选择一条不同的道路&#xff1a;在js文件中引入了css…

【牛客 - 188C】水图(bfs树的直径,思维)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/188/C 来源&#xff1a;牛客网 小w不会离散数学&#xff0c;所以她van的图论游戏是送分的 小w有一张n个点n-1条边的无向联通图,每个点编号为1~n,每条边都有一个长度 小w现在在点x上 她想知道从点x出…

5可视化数据大屏模板_可视化大屏模板分享

3个月前的一天&#xff0c;老板找到我&#xff1a;“小王&#xff0c;数据怎么才能产生让人惊艳的感觉呢&#xff1f;”我说&#xff1a;“肯定是用代码让程序员操作一下&#xff0c;再让设计师做一下配色&#xff0c;最好还能是数据实时变化的那种&#xff0c;简直就和电影里一…

delphi webbrowser 显示 html,delphi webbrowser

delphi 怎么判断webbrowser打开网页成功?unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, OleCtrls, SHDocVw; type TForm1 class(TForm) WebBrowser1: TWebBrowser; Button1: TButton; Edit1: …

【蓝桥杯官网训练 - 历届试题】对局匹配(dp,思维,取模)

题干&#xff1a; 问题描述 小明喜欢在一个围棋网站上找别人在线对弈。这个网站上所有注册用户都有一个积分&#xff0c;代表他的围棋水平。   小明发现网站的自动对局系统在匹配对手时&#xff0c;只会将积分差恰好是K的两名用户匹配在一起。如果两人分差小于或大于K&#…

c#12星座速配代码_白羊座今日运势|2020/12/11

整体运势&#xff1a;★★★☆☆爱情运势&#xff1a;★★☆☆☆事业运势&#xff1a;★★☆☆☆财富运势&#xff1a;★★★☆☆幸运数字&#xff1a;7速配星座&#xff1a;金牛座幸运颜色&#xff1a;橙色幸运时刻&#xff1a;12:00-14:00整体运势&#xff1a;接收的消息会比…

计算机网络基础端口号,1 计算机网络基础练习

Ch1 计算机网络基础1.以下网络结构当中哪一种的安全性和保密性较差A. mesh(网状)B. treeC. busD. star2.OSI 参考模型中的OSI 表示的是A. Organization Standard InstituteB. Organization Standard InterconnectionC. Open System InternetD. Open System Interconnection3.多…

【ZOJ - 2969】Easy Task (模拟,数学)

题干&#xff1a; Calculating the derivation of a polynomial is an easy task. Given a function f(x) , we use (f(x)) to denote its derivation. We use x^n to denote xn. To calculate the derivation of a polynomial, you should know 3 rules: (1) (C)0 where C is…