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

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

34 8 2

output

2

input

33 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不可能有重叠
枚举每个数可能到得所有值,以及统计达到该值的时候已经走的步数
最终答案就是1到up中num[i]最小的数
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX = 1e5 + 10;

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, 0, sizeof(num));
        memset(cnt, 0, sizeof(cnt));
        for(int i = 1; i <= n ; i++)
            scanf("%d", &a[i]);
        for(int i = 1; i <= n ; i++){
            int x = a[i];
            int pre = 0;
            while(x){
                int s = 0;
                while(x % 2 == 0){
                    x /= 2;
                    s++;//从当前偶数到最后的奇数移动的步数
                }
                int y = x;
                int x1 = 0;
                while(y <= up){
                    cnt[y]++;//可以得到的值
                    num[y] += pre + abs(s - x1);
                    x1++;
                    y *= 2;
                }
                pre += s + 1;//达到该值已经走过的步数,在接着处理一步+1
                x /= 2;
            }
        }
        int ans = inf;
        for(int i = 1; i <= up ;i++){
            if(cnt[i] == n){
                ans = min(ans, num[i]);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

  

时间: 2024-12-29 12:21:55

Codeforces Round #312 (Div. 2)——C暴力技巧——Amr and Chemistry的相关文章

Codeforces Round #224 (Div. 2) D 暴力搜索加记忆化

题意读了半年,英语太渣,题意是摆两个棋子在棋盘上作为起点,但是起点不能在#上,然后按照图的指示开始走, < 左 > 右 ^上 v下,走的时候只能按照图的指示走,如果前方是 #的话,可以走进去,但是 走进去之后便不能再走了,走的途中两个棋子不能相碰,但是最终都走到同一个#里是没事的,并且若是能走 无限步的话 输出 -1, 例如  > < 这样左右左右的走就能无限走,然后问你 两个棋子走的最大步数的和 一开始被输出-1给困住了,因为除了 .> <这样以外  还可以刚好形成一

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/contest/558/problem/A Description Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous

Codeforces Round #253 (Div. 2)B(暴力枚举)

就暴力枚举所有起点和终点就行了. 我做这题时想的太多了,最简单的暴力枚举起始点却没想到...应该先想最简单的方法,层层深入. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<map> #include<set> #include<vector> #include<

Codeforces Round #258 (Div. 2)C(暴力枚举)

就枚举四种情况,哪种能行就是yes了.很简单,关键是写法,我写的又丑又长...看了zhanyl的写法顿时心生敬佩.写的干净利落,简直美如画...这是功力的体现! 以下是zhanyl的写法,转载在此以供学习: #include <vector> #include <list> #include <queue> #include <map> #include <set> #include <deque> #include <stac

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. Lala Land is famous with its apple trees growing everywhere. Lala Land has exactly n apple trees. Tree number i is located in a position xi and has ai

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

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

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. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some

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

[比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地.拉拉土地是一个非常美丽的国家,位于坐标线.拉拉土地是与著名的苹果树越来越随处可见. 拉拉土地恰好n苹果树.树数i位于位置xi和具有人工智能的苹果就可以了增长.阿姆鲁希望从苹果树收集苹果. AMR目前维持在X =0的位置.在开始的时候,他可以选择是否去左边或右边.他会在他的方向继续下去,直到他遇见一棵苹果树,他之前没有参观.他会采取所有的苹果,然后扭转他的方向,继续走这

Codeforces Round #312 (Div. 2)

A. Lala Land and Apple Trees 题目描述: 一条坐标轴,在坐标轴上散布了一些苹果树,每棵树都有位置和所结果实数目两个属性,Amr在坐标轴0点的位置,Amr在开始的时候可以选择向左或者右走,然后遇到果树才能改变方向,问最后最多能拿到多少个苹果? 解题思路: 开两个数组,分别代表坐标正半轴和负半轴,然后对每个数组排下一序,刚开始选择苹果树多的方向,然后累加就好. 1 #include <bits/stdc++.h> 2 using namespace std; 3 str