Codeforces 558 C. AMR and Chemistry && 51NOD 1483 Chemical Transformations (violence + greed)

Source: Internet
Author: User

Portal
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 have an initial volume of AI liters. For this experiment, the AMR have to mix all the chemicals together, but all the chemicals volumes must is equal first. So he task 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 contains n space separated integers ai (1?≤?ai?≤?105), representing the initial volume of the i-th Chemica L in liters.

Output
Output One integer The minimum number of operations required to make all the chemicals volumes equal.

Examples
Input
3
4 8 2
Output
2
Input
3
3 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.

Main topic:
There are n different kinds of chemical reagents. Type I has AI L. Every experiment has to mix all the chemicals together, but the amount of these reagents must be equal. So the primary task now is to make the amount of these chemicals equal.
There are two kinds of actions:
· Double the amount of the first I, that is, the amount of type I becomes 2ai.
· Halve the amount of I, except when it is rounded down, that is, the amount of the first I is changed into? Ai/2?.
Now that the amount of all chemical reagents is known, ask at least how many times to change the amount of these chemical reagents will be equal.
Example explanation: Turn 8 into 4 and turn 2 to 4. It takes two times to get there.

Problem Solving Ideas:
We can do one initialization, that is, to enumerate the numbers that each number can reach and to record the number of steps that are required to reach that number, and then to select the minimum number of steps from the numbers that arrive at n times is what we require. Because we can get the number we want by multiplying by 2 or dividing it, if it's odd, we have to divide it by 2, and then multiply it by 2 until we reach the maximum value. Using two arrays to record, vis[] records are converted from several numbers, step[] records this number by how many to convert from, finally find vis[i]==n value, and then take step[] the smallest value output.

My Code:

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace STD;Const intMAXN =1e6+5;Const intINF =0x3f3f3f3f;intSTEP[MAXN], VIS[MAXN], Max;voidInit (intx) {intTMP1 = x, tmp2 = x, Tmp3 =0;intStep1 =0, Step2 =0, Step3 =0; while(1)    {if(Tmp1 > Max) Break; tmp1<<=1;        vis[tmp1]++;        step1++;    STEP[TMP1] + = Step1; } while(TMP2) {if(tmp2&1&& tmp2!=1) {tmp2>>=1;            vis[tmp2]++;            step2++;            STEP[TMP2] + = STEP2;            Tmp3 = TMP2; Step3 = Step2; while(1)            {if(Tmp3 > Max) Break; tmp3<<=1;                vis[tmp3]++;                step3++;            Step[tmp3] + = STEP3; }        }Else{tmp2>>=1;            vis[tmp2]++;            step2++;        STEP[TMP2] + = STEP2; }    }}intA[MAXN];intMain () {intN while(~scanf("%d", &n)) {Max =-1;memset(Vis,0,sizeof(VIS));memset(Step,0,sizeof(step)); for(intI=0; i<n; i++) {scanf("%d", &a[i]); Vis[a[i]] =1;if(Max < a[i])        Max = A[i]; } for(intI=0; i<n; i++) Init (A[i]);intans = INF; for(intI=1; i<=max*2; i++) {if(Vis[i] = = N) {if(Step[i] < ans) ans = step[i]; }        }printf("%d\n", ans); }return 0;}

Codeforces 558 C. AMR and Chemistry && 51NOD 1483 Chemical Transformations (violence + greed)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.