点击打开链接

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.

Sample test(s)
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.

给出一串数 每个数可进行乘二或除二(向下取整)操作 每次操作操作数加一

问要将所有数变成一样需要的最少操作数

由于数范围比较小

就可以直接暴力每个数进行操作后能到达的数和到达这个数需要的操作量

然后对于cnt[i]==n的数中的操作数找出最小

#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 222222
#define INF 0x3f3f3f3f
using namespace std;
int cnt[MAXN],step[MAXN];
int a[MAXN];
void solve(int num){int xx=num;int res=0;cnt[num]++;while(xx<=100000){xx*=2;cnt[xx]++;res++;step[xx]+=res;}xx=num,res=0;while(xx>1){if(xx&1){int xres=res+1;int xxx=xx/2;while(xxx<=100000){xxx*=2;xres++;step[xxx]+=xres;cnt[xxx]++;}}xx/=2;res++;cnt[xx]++;step[xx]+=res;}
}
int main(){int n;memset(cnt,0,sizeof(cnt));memset(step,0,sizeof(step));scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]);for(int i=1;i<=n;i++)solve(a[i]);int ans=INF;for(int i=1;i<MAXN;i++){if(cnt[i]==n){ans=min(ans,step[i]);}}printf("%d\n",ans);return 0;
}

Codeforces 558C Amr and Chemistry 暴力 - -相关推荐

  1. 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry

    C. Amr and Chemistry Problem's Link: http://codeforces.com/problemset/problem/558/C Mean: 给出n个数,让你通过 ...

  2. !codeforces 558C Amr and Chemistry-yy题-(位运算相关)

    题意:有n个数,每次进行的操作只能是除以2或者乘以2,求这n个数转换成同一个数字所需要的最小的操作步数 分析: 乍一看题目,觉得好难,对于这种每次有两种情况求最后到达的终点的balabala的我就觉得 ...

  3. 【CodeForces - 558C】Amr and Chemistry(位运算,bfs,计数,思维,tricks)

    题干: Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting expe ...

  4. [ An Ac a Day ^_^ ] CodeForces 691F Couple Cover 花式暴力

    Couple Cover Time Limit: 3000MS   Memory Limit: 524288KB   64bit IO Format: %I64d & %I64u Descri ...

  5. Codeforces gym 100685 A. Ariel 暴力

    A. Ariel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Des ...

  6. Codeforces 460E Roland and Rose(暴力)

    题目链接:Codeforces 460E Roland and Rose 题目大意:在以原点为圆心,半径为R的局域内选择N个整数点,使得N个点中两两距离的平方和最大. 解题思路:R最大为30.那么事实 ...

  7. 枚举 ---- B. Power Sequence[Codeforces Round #666 (Div. 2)][暴力]

    B. Power Sequence 有 n 个数,现在要求将这个数列变成一个等比数列的形式 你可以将这 n 个数随意排列 或者将任意一个数加一或者减一操作,每次此类操作都要花费 1,问最少花费是多少 ...

  8. Codeforces 1138B Circus (构造方程+暴力)

    题意: 给你两个01串,要你选n/2个位置,使得选的位置在s1中"1"的数量等于未选的s2中"1"的数量 n<=5000,1s 思路: 设两个串中出现&q ...

  9. CodeForces - 1457D XOR-gun(位运算+暴力)

    题目链接:点击查看 题目大意:给出一个长度为 n 的非降序列,现在可以执行数次操作:选择相邻的两个位置将其替换成异或和,更具体的,每次操作可以选择一个位置 i ,满足 1 <= i < n ...

最新文章

  1. 鹅厂2020暑期实习第二次二面
  2. (0002) iOS 开发之开发者iOS 10 正式版体验报告
  3. 网络推广——企业新站还得是专业网络推广公司来维护!
  4. python教程是用什么博客写的-用Python和Pygame写游戏-从入门到精通(目录)
  5. codeblock在linux中安装 出错
  6. Oracle数据库空间突然增大,Oracle 表空间异常增长过快解决方法
  7. dos 退出for循环_循环?一次帮你搞定!
  8. 算法分析中的空间复杂度
  9. mono:利用wxWindows开发界面程序
  10. Python实现Mean Shift聚类算法
  11. C51软件精确延时(us级)
  12. srt文件的时间轴平移处理
  13. 直播电商如何才能“酒香不怕巷子深”?
  14. elasticsearch6.5.4破解白金版xpack
  15. 电子邮件营销中的邮件主题设计
  16. 京东、京东全球购、考拉等电商平台入驻费用详解
  17. 情感分析的分类,情感分析模型有哪些,情感分析的应用场景,情感分析的发展趋势
  18. C#实现全盘扫描,找到符合要求的文件,并把路径写入到TXT中
  19. Mysql table_definition_cache
  20. 简单版四人扑克牌(python)

热门文章

  1. 2 理解网络协议的工作模式
  2. 2021/5/12爬虫第十一次课(ajax、selenium、Phantomjs)
  3. 统计正数和负数的个数用C语言数组,输入10个整数统计其中的正数和负数的个数,用数组实现...
  4. python中key什么意思_Python中字典的key都可以是什么
  5. 我给了老公3个月的花心期!爱还会再回来吗?
  6. 华为云618年中钜惠,服务器免费领三个月
  7. CATIA CAA二次开发专题(三)---------创建自己的Workbench
  8. PMP有效期三年后,还有必要续证吗?
  9. HashMap是什么?HashMap的底层原理是什么?
  10. ettercap的应用:DNS欺骗