【23.39%】【codeforces 558C】Amr and Chemistry

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.

Examples
input
3
4 8 2
output
2
input
3
3 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.

【题目链接】:http://codeforces.com/contest/558/problem/C

【题解】

先搞出来每个数字能变成什么数字(最大到1e5就好,不放心就加大一点);
数字能够乘2也能除2;
但不是猛地一直乘或一直除;
如果数字是
3
则3/2=1
但是这个时候
2 4 8 16都能达到了;
即1*2,1*2*2,1*2*2*2….
则除的时候要多判断一下这个数是不是奇数;
如果是奇数它除完之后还能一直乘.
因为最后要求所有的数字都变成同一个数字;
则我们在处理每一个数字到达的数字的时候,可以直接累加这个数字要经过多少步到达;
最后O(N)枚举求最小值就好;(n个数字都能变成这个数字的话);
用map会T…

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
#define pri(x) printf("%d",x)
#define prl(x) printf("%I64d",x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int MAXN = 1e5+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int n;
int a[MAXN];
int dic[MAXN],dic3[MAXN];

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    rei(n);
    rep1(i,1,n)
        rei(a[i]);
    rep1(i,1,n)
    {
        int step = 0;
        int now = a[i];
        dic[now]++;
        dic3[now]+=step;
        now<<=1;
        while (now <= 1e5)
        {
            dic[now]++;
            step++;
            dic3[now]+=step;
            now<<=1;
        }
        now = a[i];
        step = 0;
        while (now >0)
        {
            if (now&1)
            {
                int tnow = now>>1;
                if (tnow<=0) break;
                int tstep = step+1;
                tnow <<=1;
                tstep++;
                while (tnow <= 1e5)
                {
                    dic[tnow]++;
                    dic3[tnow]+=tstep;
                    tstep++;
                    tnow<<=1;
                }
            }
            now>>=1;
            if (now <=0) break;
            step++;
            dic[now]++;
            dic3[now]+=step;
        }
    }
    int ans = 21e8;
    rep1(i,0,1e5)
    if (dic[i]==n)
            ans = min(ans,dic3[i]);
    pri(ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值