zoukankan      html  css  js  c++  java
  • Amr and Chemistry

    C. Amr and Chemistry

    time limit per test

    1 second

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard 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.

    //题意是,给你n个整数,每一个整数可以进行两种操作,除2(取整)或者乘2.每个整数可以进行任意次这样的操作。

    使这n个整数都变为相同的整数最少需要多少次操作。

    //暴力bfs   1292kb 607ms

    //还是需要一点技巧的...

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <queue>
     4 #include <string.h>
     5 using namespace std;
     6 
     7 int n,max_;
     8 int num[100005];
     9 int times[100005];//有几个值可以到这个数
    10 int vis[100005];//所有值变成这个值需要的操作数
    11 
    12 struct Step
    13 {
    14     int e;//
    15     int s;//步数
    16 };
    17 
    18 bool v[100005];//暂时用来bfs的
    19 void func(int x)
    20 {
    21     queue<Step> Q;
    22     memset(v,0,sizeof(v));
    23 
    24     Step k;
    25     k.e=x;
    26     k.s=0;
    27 
    28     Q.push(k);
    29     times[k.e]++;
    30     v[k.e]=1;
    31 
    32     while (!Q.empty())
    33     {
    34         k=Q.front();
    35         Q.pop();
    36         Step next;
    37         next.e=k.e*2;
    38         next.s=k.s+1;
    39         if (next.e<=100000&&v[next.e]==0)
    40         {
    41             vis[next.e]+=next.s;
    42             times[next.e]++;
    43             v[next.e]=1;
    44             Q.push(next);
    45         }
    46         next.e=k.e/2;
    47         if (next.e>=1&&v[next.e]==0)
    48         {
    49             vis[next.e]+=next.s;
    50             times[next.e]++;
    51             v[next.e]=1;
    52             Q.push(next);
    53         }
    54     }
    55 }
    56 
    57 int main()
    58 {
    59     scanf("%d",&n);
    60     int i,j;
    61     for (i=0;i<n;i++)
    62         scanf("%d",&num[i]);
    63 
    64     //memset(vis,0,sizeof(vis));
    65     //memset(times,0,sizeof(times));
    66     for (i=0;i<n;i++)
    67     {
    68         func(num[i]);//每个点可以去的地方
    69     }
    70     int ans=1e8;
    71     for (i=1;i<=100000;i++)
    72     {
    73         if (times[i]==n&&vis[i]<ans)//那个值必须要有n个数可以到,
    74             ans=vis[i];
    75     }
    76     printf("%d
    ",ans);
    77     return 0;
    78 }
    View Code

    更好的做法  984kb 46ms

    //别人的,还未仔细看...

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cmath>
     5 using namespace std;
     6 const int MAX = 1e5 + 10;
     7  
     8 int num[MAX];
     9 int cnt[MAX];
    10 int a[MAX];
    11 const int up = 1e5;
    12 const int inf = 0x3f3f3f3f;
    13 int main()
    14 {
    15     int n;
    16     while(~scanf("%d", &n)){
    17         memset(num, 0, sizeof(num));
    18         memset(cnt, 0, sizeof(cnt));
    19         for(int i = 1; i <= n ; i++)
    20             scanf("%d", &a[i]);
    21         for(int i = 1; i <= n ; i++){
    22             int x = a[i];
    23             int pre = 0;
    24             while(x){
    25                 int s = 0;
    26                 while(x % 2 == 0){
    27                     x /= 2;
    28                     s++;//从当前偶数到最后的奇数移动的步数
    29                 }
    30                 int y = x;
    31                 int x1 = 0;
    32                 while(y <= up){
    33                     cnt[y]++;//可以得到的值
    34                     num[y] += pre + abs(s - x1);
    35                     x1++;
    36                     y *= 2;
    37                 }
    38                 pre += s + 1;//达到该值已经走过的步数,在接着处理一步+1
    39                 x /= 2;
    40             }
    41         }
    42         int ans = inf;
    43         for(int i = 1; i <= up ;i++){
    44             if(cnt[i] == n){
    45                 ans = min(ans, num[i]);
    46             }
    47         }
    48         printf("%d
    ", ans);
    49     }
    50     return 0;
    51 }
    View Code
  • 相关阅读:
    jquery_ajax 地址三级联动
    delphi窗体按钮灰化禁用
    sqlserver查找断号,回收单据号
    query.locate过个过滤参数
    StringGrid换行功能
    你不知道的JavaScript--Item13 理解 prototype, getPrototypeOf 和__proto__
    你不知道的JavaScript--Item12 undefined 与 null
    你不知道的JavaScript--Item11 arguments对象
    你不知道的JavaScript--Item10 闭包(closure)
    你不知道的JavaScript--Item9 call(),apply(),bind()与回调
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/6087092.html
Copyright © 2011-2022 走看看