Codeforces 558C. Amr and Chemistry(暴搞)

来源:互联网 发布:海淘比价软件 编辑:程序博客网 时间:2024/05/21 18:46

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
34 8 2
output
2
input
33 5 6
output

5

solution:

强行枚举它所能到达的0~maxn的步数,其中有些技巧,看代码即可

#include<cstdio>#include<queue>using namespace std;#define mp make_pairconst int maxn = 1e5 + 20;int vis[maxn], step[maxn], ans[maxn];int main(){int n, x, y;scanf("%d", &n);for (int i = 1; i <= n; i++){scanf("%d", &x);queue< pair<int, int> >que;que.push(mp(x, 0));while (!que.empty()){x = que.front().first;y = que.front().second;que.pop();if (x >= maxn||vis[x]==i)continue;vis[x] = i;ans[x]++;step[x] += y;que.push(mp(2 * x, y + 1));que.push(mp(x / 2, y + 1));}}int tmp = 1e9;for (int i = 0; i < maxn; i++)if (ans[i] == n&&tmp > step[i])tmp = step[i];printf("%d\n", tmp);return 0;}


0 0
原创粉丝点击