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.

Sample test(s)
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。问至少操作多少次使得全部数相等。

点击打开链接

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h> using namespace std; int a[1000050];
int num[1000010];
int v[10000060];
int maxx;
int n; int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(num,0,sizeof(num));
memset(v,0,sizeof(v));
maxx = -1;
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
v[a[i]]++;
if(maxx<a[i])
{
maxx = a[i];
}
}
for(int i=0; i<n; i++)
{
int t = a[i];
int cnt = 0;
while(t<maxx)
{
t = t<<1;
cnt++;
v[t]++;
num[t] += cnt;
}
cnt = 0;
int pp;
t = a[i];
while(t>0)
{
int flag = 0;
if(t%2 == 1 && t!=1)
{
flag = 1;
}
t = t>>1;
cnt++;
v[t]++;
num[t] += cnt;
if(flag == 1)
{
pp = t;
int pcnt = cnt;
while(pp<maxx)
{
pp = pp<<1;
pcnt++;
v[pp]++;
num[pp] += pcnt;
} }
}
}
int minn = 9999999;
for(int i=0; i<=maxx; i++)
{ if(v[i] == n && minn > num[i])
{
minn = num[i];
}
}
printf("%d\n",minn);
}
return 0;
}

C. Amr and Chemistry(Codeforces Round #312 (Div. 2) 二进制+暴力)的更多相关文章

  1. 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/ ...

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

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

  3. Codeforces Round #312 (Div. 2) ABC题解

    [比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地. 拉拉土地是一个很漂亮的国家,位于坐标线.拉拉土地是与著名的苹果树越来 ...

  4. Codeforces Round #312 (Div. 2)

    好吧,再一次被水题虐了. A. Lala Land and Apple Trees 敲码小技巧:故意添加两个苹果树(-1000000000, 0)和(1000000000, 0)(前者是位置,后者是价 ...

  5. Codeforces Round #312 (Div. 2)B. Amr and The Large Array 暴力

    B. Amr and The Large Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  6. Codeforces Round #312 (Div. 2) B.Amr and The Large Array

    Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. ...

  7. B. Amr and The Large Array(Codeforces Round #312 (Div. 2)+找出现次数最多且区间最小)

    B. Amr and The Large Array time limit per test 1 second memory limit per test 256 megabytes input st ...

  8. Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees 暴力

    A. Lala Land and Apple Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/cont ...

  9. Codeforces Round #312 (Div. 2) A.Lala Land and Apple Trees

    Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. ...

随机推荐

  1. rabbitMQ学习笔记(五) 消息路由

    生产者会生产出很多消息 , 但是不同的消费者可能会有不同的需求,只需要接收指定的消息,其他的消息需要被过滤掉. 这时候就可以对消息进行过滤了. 在消费者端设置好需要接收的消息类型. 如果不使用默认的E ...

  2. static final常量变量的正确书写规范

    AccountConstants.java类 命名:常量类以Constants单词命名结尾 package com.paic.pacz.core.salesmanage.util; import ja ...

  3. 剑指Offer读书笔记(持续更新中)

    (1)定义一个空的类型,里面没有不论什么成员变量和成员函数,对该类型求sizeof,得到的结果是多少? 答案是1.空类型的实例中不包括不论什么信息,本来求sizeof应该是0,可是当我们声明该类型实例 ...

  4. [HTML5] aria-label & aria-labelledby

    'aria-labelledby' overwrite 'aria-label' overwirte native element label. TOP-LEFT: aria-label overwr ...

  5. python 学习笔记 12 -- 写一个脚本获取城市天气信息

    近期在玩树莓派,前面写过一篇在树莓派上使用1602液晶显示屏,那么可以显示后最重要的就是显示什么的问题了. 最easy想到的就是显示时间啊,CPU利用率啊.IP地址之类的.那么我认为呢,假设可以显示当 ...

  6. HIbernate中openSession和getCurrentSession

      这两者的差别网上非常多资源,我这里就copy一下了,然后有点问题的是今天遇到的问题.   openSession和getCurrentSession的根本差别在于有没有绑定当前线程,所以,用法有差 ...

  7. Java基础之关键字

    一.Java关键字总览 基本数据类型(9):boolean.char.byte.short.int.long.float.double.null 变量引用(2):super.this 类.方法.变量修 ...

  8. TLP电源管理

    笔记本电脑电池坏了, 换了块电池, 顺手装了一下这个电源管理软件.   https://linrunner.de/en/tlp/docs/tlp-linux-advanced-power-manage ...

  9. 算法入门经典-第七章 例题7-4-1 拓展 n皇后问题 回溯法

    实际上回溯法有暴力破解的意思在里面,解决一个问题,一路走到底,路无法通,返回寻找另   一条路. 回溯法可以解决很多的问题,如:N皇后问题和迷宫问题. 一.概念 回溯算法实际类似枚举的搜索尝试过程,主 ...

  10. C#、SQL中的事务

    c#方法一: TransactionOptions transactionOption = new TransactionOptions(); //设置事务隔离级别 transactionOption ...