Links: Http://codeforces.com/problemset/problem/558/C
C. AMR and Chemistrytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard 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 a i liters. For this experiment, the AMR have to mix all the chemicals together, but all the chemicals volumes must is equal first. So he is the chemicals volumes equal.
To does this, the AMR can do different kind of operations.
- Choose some chemical i and double its current volume so the new volume would be 2ai
- Choose some chemical i and divide its volume by II (integer division) so the new volume would be
Suppose that each chemical are contained in a vessel of infinite volume. Now AMR wonders what's 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 containsNSpace separated integers ai (1?≤? a i? ≤?105 ), representing the initial volume of theI-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 are to divide the second chemical volume by both, and multiply the third chem ical volume by and volumes equal 4.
In the second sample test, the optimal solution are to divide the first chemical volume by both, and divide the second and T He third chemical volumes by and twice to make all the volumes equal 1.
Test instructions
Give you a few numbers.
Each number can be removed or multiplied by the operation. 3/2=1
Ask at least how many steps to make all the numbers equal.
Practice:
First, if all becomes a number, then this number must be less than or equal to the largest of those numbers.
Here you can think of each number as a binary.
Such as
One (D)
1101 (2)
So you can actually enumerate any number.
Any binary can move to the left a number of bits, to get a new number.
The odd number of occurrences is log (n), and the left movement is also log (n) so the complexity is log^2 (n) *n
or move right.
You can know that if you move the odd number to the right, the binary will be 1 less, so the numbers that appear when you move to the left are not there before.
Record the number of steps for each occurrence, record the number of occurrences, and finally count the minimum number of steps to reach N for each occurrence.
int Ci[101000];int Buhe[101000];int Main () {int N;int lim=100000;while (scanf ("%d", &n)!=eof) {memset (ci,0,sizeof ci ); memset (buhe,0,sizeof Buhe); for (int i=0;i<n;i++) {int num;scanf ("%d", &num); int flag=1;int bu=0; while (num) {if (flag) {int tt=bu+1;int num2=num*2;; while (Num2<=lim) {ci[num2]++;buhe[num2]+=tt;num2*=2;tt++;} flag=0;} Ci[num]++;buhe[num]+=bu;bu++;if (num%2==0) num/=2;else{flag=1;num/=2;}} int minn=999999999;for (int i=1;i<=lim;i++) {if (ci[i]==n) minn=min (Minn,buhe[i]);} printf ("%d\n", Minn);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
CF 558 C. AMR and chemistry violence + binary