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 ai liters. 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.

Example

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 倍, 二是将此数变为 一半。

思路分析 :

  最初想的是 广搜, n 个数, 数据范围是 1e5 ,每次乘以 2 ,总的操作数加起来也没有多少,所以对每个数都进行广搜,但是如果只是单纯这么写一直都是超时,后来一位 丁学长指导了下, 每次记步数的数组清空只清空之前用过的地方,这个地方一优化题目就直接过了。

const int eps = 1e5+5;
const double pi = acos(-1.0);
const int inf = 1<<29;
#define Max(a,b) a>`b?a:b
#define Min(a,b) a>b?b:a
#define ll long long int pre[eps];
int bu[eps];
int pt[eps];
int p[eps]; queue<int>que;
stack<int>s;
int maxn = 0x3f3f3f3f;
void bfs(int num){
while(!que.empty()) que.pop(); que.push(num);
p[num] = 0;
s.push(num);
while(!que.empty()) {
int x = que.front();
que.pop(); int x1 = x / 2;
int x2 = x*2;
if (x1 >= 1 && x1 <= maxn && p[x1] == -1) {
p[x1] = p[x] + 1;
que.push(x1);
bu[x1] += p[x1];
pt[x1]++;
s.push(x1);
}
if (x2 <= eps && x2 <= maxn && p[x2] == -1){
que.push(x2);
p[x2] = p[x] + 1;
bu[x2] += p[x2];
pt[x2]++;
s.push(x2);
}
}
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n; cin >> n; for(int i = 1; i <= n; i++){
scanf("%d", &pre[i]);
maxn = max(maxn, pre[i]);
}
memset(p, -1, sizeof(p));
for(int i = 1; i <= n; i++){
pt[pre[i]]++;
bfs(pre[i]);
while(!s.empty()) {
int x = s.top();
s.pop();
p[x] = -1;
}
}
int ans = 0x3f3f3f3f;
for(int i = 1; i <= 100000; i++){
if (pt[i] == n) {
ans = min(ans, bu[i]);
}
}
printf("%d\n", ans);
return 0;
}

题目还有种方法就是直接暴力,队于每个数如果它是奇数,你除以 2 在乘以 2 ,会得到一组新的不同的数,但是如果是偶数的此过程就没有意义。

const int eps = 1e5+5;
const double pi = acos(-1.0);
const int inf = 1<<29;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long int pre[eps];
int bu[eps];
int cnt[eps]; void fun(int x) { int p = x;
int k = 0;
while(p <= 100000){
cnt[p]++;
bu[p] += k++;
p *= 2;
}
p = x;
k = 0; while(p > 1) {
int f = p/2;
k++;
cnt[f]++;
bu[f] += k; if (p & 1) {
int ff = f*2;
int kk = k+1;
while(ff <= 100000){
cnt[ff]++;
bu[ff] += kk++;
ff *= 2;
}
}
p /= 2;
}
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n;
cin >> n; for(int i = 1; i <= n; i++){
scanf("%d", &pre[i]);
}
for(int i = 1; i <= n; i++){
fun(pre[i]);
}
int ans = 0x3f3f3f3f; for(int i = 1; i <= 100000; i++){
if (cnt[i] == n) {
ans = min(ans, bu[i]);
}
}
printf("%d\n", ans);
return 0;
}

CF 558 C的更多相关文章

  1. CF 558 C. Amr and Chemistry 暴力+二进制

    链接:http://codeforces.com/problemset/problem/558/C C. Amr and Chemistry time limit per test 1 second ...

  2. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  3. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  4. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  5. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  6. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  7. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  8. CF memsql Start[c]UP 2.0 B

    CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...

  9. CF #376 (Div. 2) C. dfs

    1.CF #376 (Div. 2)    C. Socks       dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...

随机推荐

  1. H3C STP基本配置

  2. linux 自动检测 IRQ 号

    驱动在初始化时最有挑战性的问题中的一个是如何决定设备要使用哪个 IRQ 线. 驱动需 要信息来正确安装处理. 尽管程序员可用请求用户在加载时指定中断号, 这是个坏做法, 因为大部分时间用户不知道这个号 ...

  3. [USACO10OCT]Lake Counting(DFS)

    很水的DFS. 为什么放上来主要是为了让自己的博客有一道DFS题解,,, #include<bits/stdc++.h> using namespace std; ][],ans,flag ...

  4. jedis 连接池工具类

    maven <properties> <jedis.version>3.0.1</jedis.version> <junit.verion>4.12&l ...

  5. 查看虚拟机里的Centos7的IP(设置centos网卡)

    这里之所以是查看下IP ,是我们后面要建一个Centos远程工具Xshell 连接Centos的时候,需要IP地址,所以我们这里先 学会查看虚拟机里的Centos7的IP地址 首先我们登录操作系统 用 ...

  6. 微信小程序获取多个input和textarea的值(es6加微信小程序APi)

    wxml js 俩行即可解决,money1,money2,money3

  7. 第二阶段:2.商业需求分析及BRD:1.产品需求管理

    产品经理需要投入大量的时间在需求方面的工作. 一张图看出需求多么重要.各个方面的人物对需求的误解导致的后果. 首先收集需求 需求采集 可以用excel收集数据并整理 备注信息也很重要 有时候要跟提出人 ...

  8. nginx实现缓存功能

    Nginx从0.7.48版本开始,支持了类似Squid的缓存功能.这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302 ...

  9. Linux 学习笔记 4 创建、复制、移动、文件的基本操作

    写在前面 通过上一节的学习,我们基本的了解到在Linux 里面对于设备的挂载.卸载以及设备存在的目录.挂载目录.都有了一个基本的了解 本节主要了解文件.以及目录的相关操作,比如文件.目录的创建.以及删 ...

  10. 20行Python代码爬取王者荣耀全英雄皮肤

    引言王者荣耀大家都玩过吧,没玩过的也应该听说过,作为时下最火的手机MOBA游戏,咳咳,好像跑题了.我们今天的重点是爬取王者荣耀所有英雄的所有皮肤,而且仅仅使用20行Python代码即可完成. 准备工作 ...