Title Link: Http://codeforces.com/problemset/problem/558/C
Test instructions: Change n number to the same minimum number of steps required to go
Easy to get the conclusion that two odd numbers are different and have been x2 impossible to overlap
Enumerate the possible values of each number and the number of steps that have been taken when the value is reached
The final answer is 1 to up num[i] the smallest number
Examplesinput34 8 2Output2
input33 5 6Output5
Notein The first sample test, the optimal solution isTo divide the second chemical volume by both, and multiply the third chemical volume by both to make all the volumes equal4. In the second sample test, the optimal solution isTo divide the first chemical volume by A and divide the second and the third chemical volumes by both twice to do all The volumes equal1.
***********************************************************
AC Code:
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5#include <iostream>6#include <queue>7#include <map>8#include <stdlib.h>9 Ten using namespacestd; One A #defineN 5200 - #defineINF 0x3f3f3f3f - #defineMAXN 100010 the - intA[MAXN],STEP[MAXN],NUM[MAXN]; - - intMain () + { - intn,i; + A while(SCANF ("%d", &n)! =EOF) at { -memset (step,0,sizeof(step)); -memset (NUM,0,sizeof(num)); - - for(i=1; i<=n; i++) -scanf"%d", &a[i]); in - for(i=1; i<=n; i++) to { + intX=a[i],sc=0; - the while(x) * { $ ints1=0;Panax Notoginseng while(%2==0) - { theX/=2; +s1++; A } the + intY=x,s2=0; - while(y<=MAXN) $ { $num[y]++; -Step[y]+=sc+abs (s1-S2); -s2++; they*=2; - }Wuyi thesc+=s1+1; -X/=2; Wu } - } About intans=INF; $ for(i=1; i<=maxn;i++) - if(num[i]==N) -ans=min (ans,step[i]); - Aprintf"%d\n", ans); + } the return 0; -}
Codeforces 558/c AMR and Chemistry