C. Amr and Chemistry

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

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   1292kb 607ms

//还是需要一点技巧的...

 #include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std; int n,max_;
int num[];
int times[];//有几个值可以到这个数
int vis[];//所有值变成这个值需要的操作数 struct Step
{
int e;//值
int s;//步数
}; bool v[];//暂时用来bfs的
void func(int x)
{
queue<Step> Q;
memset(v,,sizeof(v)); Step k;
k.e=x;
k.s=; Q.push(k);
times[k.e]++;
v[k.e]=; while (!Q.empty())
{
k=Q.front();
Q.pop();
Step next;
next.e=k.e*;
next.s=k.s+;
if (next.e<=&&v[next.e]==)
{
vis[next.e]+=next.s;
times[next.e]++;
v[next.e]=;
Q.push(next);
}
next.e=k.e/;
if (next.e>=&&v[next.e]==)
{
vis[next.e]+=next.s;
times[next.e]++;
v[next.e]=;
Q.push(next);
}
}
} int main()
{
scanf("%d",&n);
int i,j;
for (i=;i<n;i++)
scanf("%d",&num[i]); //memset(vis,0,sizeof(vis));
//memset(times,0,sizeof(times));
for (i=;i<n;i++)
{
func(num[i]);//每个点可以去的地方
}
int ans=1e8;
for (i=;i<=;i++)
{
if (times[i]==n&&vis[i]<ans)//那个值必须要有n个数可以到,
ans=vis[i];
}
printf("%d\n",ans);
return ;
}

更好的做法  984kb 46ms

//别人的,还未仔细看...

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX = 1e5 + ; int num[MAX];
int cnt[MAX];
int a[MAX];
const int up = 1e5;
const int inf = 0x3f3f3f3f;
int main()
{
int n;
while(~scanf("%d", &n)){
memset(num, , sizeof(num));
memset(cnt, , sizeof(cnt));
for(int i = ; i <= n ; i++)
scanf("%d", &a[i]);
for(int i = ; i <= n ; i++){
int x = a[i];
int pre = ;
while(x){
int s = ;
while(x % == ){
x /= ;
s++;//从当前偶数到最后的奇数移动的步数
}
int y = x;
int x1 = ;
while(y <= up){
cnt[y]++;//可以得到的值
num[y] += pre + abs(s - x1);
x1++;
y *= ;
}
pre += s + ;//达到该值已经走过的步数,在接着处理一步+1
x /= ;
}
}
int ans = inf;
for(int i = ; i <= up ;i++){
if(cnt[i] == n){
ans = min(ans, num[i]);
}
}
printf("%d\n", ans);
}
return ;
}

Amr and Chemistry的更多相关文章

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

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

  2. Codeforces Round #312 (Div. 2) C. Amr and Chemistry 暴力

    C. Amr and Chemistry Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/558/ ...

  3. Codeforces Round #312 (Div. 2) C.Amr and Chemistry

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

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

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

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

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

  6. codeforces 558C C. Amr and Chemistry(bfs)

    题目链接: C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input st ...

  7. C. Amr and Chemistry(Codeforces Round #312 (Div. 2) 二进制+暴力)

    C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. 【23.39%】【codeforces 558C】Amr and Chemistry

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. Amr and Chemistry CodeForces 558C(BFS)

    http://codeforces.com/problemset/problem/558/C 分析:将每一个数在给定范围内(10^5)可变成的数(*2或者/2)都按照广搜的方式生成访问一遍,标记上访问 ...

随机推荐

  1. session的作用范围(转)

    session是在服务器端建立的,浏览器访问服务器会有一个jsessionid,浏览器端通过 jsessionid定位服务器端的session,session的创建和销毁由服务器端控制.当浏览器关闭后 ...

  2. SVM相关知识及和softmax区别

    1.相对于容易过度拟合训练样本的人工神经网络,支持向量机对于未见过的测试样本具有更好的推广能力. 2.SVM更偏好解释数据的简单模型---二维空间中的直线,三维空间中的平面和更高维空间中的超平面. 3 ...

  3. 模式识别hw2-------基于matconvnet,用CNN实现人脸图片性别识别

    主要来源模式识别课程大作业,本文首先感谢当初的助教和一起完毕作业的队友 matconvnet在matlab下封装了CNN常见算法,网址http://www.vlfeat.org/matconvnet/ ...

  4. ElasticSearch获取指定Field数据的Java方法

    ElasticSearch(ES)检索后需要结果时,可能通过source接口读出.但是这样的话,返回的结果会很多.在调用search方法时,我们可以添加addfield或addfields方法,仅仅读 ...

  5. spring 配置多数据源 (案例)

    *.properties配置: <!--数据连接配置一--> jdbc.type=mysqljdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:m ...

  6. [Tools] Create a Simple CLI Tool in Node.js with CAC

    Command-line tools can help you with all sorts of tasks. This lesson covers the very basics of setti ...

  7. mac os x+paralles使用source insight

    将Mac OS X下的目录共享到Paralles后,source insight创建工程.但是当再次打开时却打开失败.提示:there was an error opening project 网上对 ...

  8. 解决log4j.xml问题http//jakarta.apache.org/log4j/ uri is not registered

    在Eclipse中,配置log4j.xml出现"http //jakarta.apache.org/log4j/ uri is not registered"的错误信息. 原始的l ...

  9. Java 分页之最简单的算法

    分页实现有很多方式,如jQuery自带框架pagination或在java封装一个类pager等.   下写一个简单易懂的分页算法   逻辑:   // 步骤1:设置每页页数大小 long pageS ...

  10. handlebars.js基础学习笔记

    最近在帮学校做个课程网站,就有人推荐用jquery+ajax+handlebars做网站前端,刚接触发现挺高大上的,于是就把一些基础学习笔记记录下来啦. 1.引用文件: jquery.js文件下载:h ...