标签:
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.
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?
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 one integer the minimum number of operations required to make all the chemicals volumes equal.
3
4 8 2
2
3
3 5 6
5
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;}
Codeforces Round #312 (Div. 2) C.Amr and Chemistry
标签:
原文地址:http://www.cnblogs.com/chxer/p/4676798.html