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.

题解:按值爆搜交上去就满了。。。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cstring>
 7 #define PAU putchar(‘ ‘)
 8 #define ENT putchar(‘\n‘)
 9 using namespace std;
10 const int maxn=100000+10,maxv=100000,inf=-1u>>1;
11 int vis[maxn],cnt[maxn],stp[maxn],n;
12 inline int read(){
13     int x=0,sig=1;char ch=getchar();
14     while(!isdigit(ch)){if(ch==‘-‘)sig=-1;ch=getchar();}
15     while(isdigit(ch))x=10*x+ch-‘0‘,ch=getchar();
16     return x*=sig;
17 }
18 inline void write(int x){
19     if(x==0){putchar(‘0‘);return;}if(x<0)putchar(‘-‘),x=-x;
20     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
21     for(int i=len-1;i>=0;i--)putchar(buf[i]+‘0‘);return;
22 }
23 void init(){
24     n=read();
25     queue<pair<int,int> >Q;
26     for(int i=1;i<=n;i++){
27         int num=read();Q.push(make_pair(num,0));
28         while(!Q.empty()){
29             int x=Q.front().first,y=Q.front().second;Q.pop();
30             if(x>maxv||vis[x]==i)continue;
31             vis[x]=i;cnt[x]++;stp[x]+=y;
32             Q.push(make_pair(x<<1,y+1));
33             Q.push(make_pair(x>>1,y+1));
34         }
35     }
36     int mi=inf;
37     for(int i=0;i<=maxv;i++)if(cnt[i]==n&&stp[i]<mi)mi=stp[i];
38     write(mi);
39     return;
40 }
41 void work(){
42     return;
43 }
44 void print(){
45     return;
46 }
47 int main(){init();work();print();return 0;}
时间: 2024-08-06 01:03:33

Codeforces Round #312 (Div. 2) C.Amr and Chemistry的相关文章

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 #254 (Div. 2) B. DZY Loves Chemistry (并查集)

题目链接 昨天晚上没有做出来,刚看题目的时候还把题意理解错了,当时想着以什么样的顺序倒,想着就饶进去了, 也被题目下面的示例分析给误导了. 题意: 有1-n种化学药剂  总共有m对试剂能反应,按不同的次序将1-n种试剂滴入试管,如果正在滴入的试剂能与已经滴入 的试剂反应,那么危险数*2,否则维持不变.问最后最大的危险系数是多少. 分析:其实这个题根本不用考虑倒入的顺序,只是分块就行,结果就是每个子集里元素的个数-1 和  的2的幂. 1 #include <iostream> 2 #inclu

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) 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 暴力

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 #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)

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

Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II 贪心排序

D. Guess Your Way Out! II time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Amr bought a new video game "Guess Your Way Out! II". The goal of the game is to find an exit from the maze that

Codeforces Round #312 (Div. 2) E. A Simple Task

题目大意就是给一个字符串,然后多个操作,每次操作可以把每一段区间的字符进行升序或者降序排序,问最终的字符串是多少. 一开始只考虑字符串中字符'a'的情况,假设操作区间[L,R]中有x个'a',那么一次操作后,这x个'a'要么去最左(升序),要么去最右(降序),我们可以建立一颗线段树来维护这样的操作,字符'a'出现的位置值为1,否则为0,那么q次操作后,最后值为1的地方填的就是'a'了. 在考虑字符'a'和'b'的情况,操作的情况和上面类似,字符'a'和'b'出现的位置值为1,否则为0,q次操作后