C. Amr and Chemistry

Problem's Link: http://codeforces.com/problemset/problem/558/C


Mean:

给出n个数,让你通过下面两种操作,把它们转换为同一个数。求最少的操作数。

1.ai = ai*2

2.ai = ai/2 (向下取整)

analyse:

基本思路:首先枚举出每个数能够到达的数字并且记录下到达该数组需要的步数,然后从到达次数为n次的数字中选择步数最小的即为答案。

对于一个数字Ai,它可以变换得到的数字可以分为三类:

1.向左移动n位得到的数字;

2.向右移动n位得到的数字;

3.奇数/2后得到的偶数向右移动n位得到的数字。

处理一个数 Ai:

1、对 Ai 执行左移操作,记录 Ai 通过左移能够得到的数字,上限为1e5,vis 数组加1,cnt数组记录步数

2、对 Ai 执行右移操作,直到 Ai 为0.

若 Ai 的最低位为1,右移一步之后,进行左移,上限为1e5,维持vis数组和cnt数组.

若 Ai 的最低位为0,右移一步,维持vis数组和cnt数组.

这样我们就把一个数的所有情况都处理出来了,并且是最少的操作数.

最后遍历数组找 vis[i] 为n,并且操作数最小。

Time complexity: O(n*m*log(MAX))

Source code: 

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-07-15-18.53
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL long long
#define ULL unsigned long long
using namespace std; const int MAXN(+int(1e5));
int arrive_step[MAXN<<];
int arrive_num[MAXN<<];
int a[MAXN],n; void init()
{
for(int i=; i<MAXN<<; ++i) { arrive_step[i]=,arrive_num[i]=; }
} inline void count_arrive(int &x)
{
int t=x,step=;
arrive_num[t]++;
while(t<=int(1e5))
{
t<<=;
step++;
arrive_step[t]+=step;
arrive_num[t]++;
}
t=x,step=;
while(t>)
{
if(t!= && (t&))
{
int sta=t>>;
int sstep=step+;
while(sta<=int(1e5))
{
sta<<=;
sstep++;
arrive_step[sta]+=sstep;
arrive_num[sta]++;
}
}
t>>=;
step++;
arrive_step[t]+=step;
arrive_num[t]++;
}
} int main()
{
ios_base::sync_with_stdio(false);
cin.tie();
while(~scanf("%d",&n))
{
init();
for(int i=; i<n; ++i)
{
scanf("%d",&a[i]);
count_arrive(a[i]);
}
int ans=INT_MAX;
for(int i=; i<(MAXN<<); ++i)
{
if(arrive_num[i]==n)
ans=ans<arrive_step[i]?ans:arrive_step[i];
}
printf("%d\n",ans);
}
return ;
}
/* */

暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry的更多相关文章

  1. Codeforces 558C Amr and Chemistry 暴力 - -

    点击打开链接 Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input stan ...

  2. CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)

    Codeforces 558C 题意:给n个数字,对每一个数字能够进行两种操作:num*2与num/2(向下取整),求:让n个数相等最少须要操作多少次. 分析: 计算每一个数的二进制公共前缀. 枚举法 ...

  3. Codeforces 558C Amr and Chemistry 全都变相等

     题意:给定一个数列,每次操作仅仅能将某个数乘以2或者除以2(向下取整). 求最小的操作次数使得全部的数都变为同样值. 比赛的时候最后没实现.唉.之后才A掉.開始一直在想二分次数,可是半天想不出怎 ...

  4. Codeforces 558C Amr and Chemistry

    题意: n个数.每次能够选一个数 让其 *=2 或者 /=2 问至少操作多少次使得全部数相等. 思路: 对于每一个数,计算出这个数能够变成哪些数,以及变成那个数的最小步数,用两个数组保存 cnt[i] ...

  5. Codeforces Round #312 (Div. 2) C. Amr and Chemistry 暴力

    C. Amr and Chemistry Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/558/ ...

  6. codeforces 558C C. Amr and Chemistry(bfs)

    题目链接: C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input st ...

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

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. C. Amr and Chemistry(Codeforces Round #312 (Div. 2) 二进制+暴力)

    C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. CF 558 C. Amr and Chemistry 暴力+二进制

    链接:http://codeforces.com/problemset/problem/558/C C. Amr and Chemistry time limit per test 1 second ...

随机推荐

  1. CDH的几个包的下载地址

    https://archive.cloudera.com/cdh5/parcels/5.3.0/ http://archive.cloudera.com/cm5/installer/5.3.0/ ht ...

  2. 新安装的VS的一些设置

    古语云:工欲善其事必先利其器 为了方便我们开发,应该设置好VS的一些配置,安装一些辅助插件 1 设置字体和背景等 设置字体为 console 10大小 背景设为护眼颜色 85 90 205 这三个值 ...

  3. VS2010安装失败解决办法

    1. 运行regedit打开注册表: 2. 找到HKEY_LOCAL_MACHINE\SOFWARE\ Microsoft\ Internet Explorer\ MAIN: 3. MAIN子键的权限 ...

  4. Swift: 深入理解Core Animation(一)

    如果想在底层做一些改变,想实现一些特别的动画,这时除了学习Core Animation之外,别无选择. 最近在看<iOS Core Animation:Advanced Techniques&g ...

  5. 一个JAVA数据库连接池实现源码

    原文链接:http://www.open-open.com/lib/view/open1410875608164.html // // 一个效果非常不错的JAVA数据库连接池. // from:htt ...

  6. linux的文件种类与扩展名

    一.文件种类: 1.普通文件(regular file)第一个字符为[ - ] 包括:①纯文本档(ASCII):这是Linux系统中最多的一种文件类型,称为纯文本档.是因为内容为我们人类可以直接读到的 ...

  7. (转)2G到C-RAN网络架构的演进

    这是我一个学霸师弟写的一篇知识普及的文章,涉及到的知识结构很全面,很丰富,对网络通信技术感兴趣的童鞋可以看看,内容我就不贴过来了,直接到他博客上去看吧. 2G到C-RAN网络结构演进

  8. 基于Maven构建整合SpringMVC+Mybtis+Druid

    前几天趁空闲时间整合了下SpringMVC+Mybatis+Druid,这里小记录下,这个Demo是基于Maven构建的,数据源用的是阿里巴巴温少的开源项目Druid,数据库用的是Mysql. 由于E ...

  9. easyui filebox 浏览图片

    <img id="image1"/> <input id="f1" class="easyui-filebox" name ...

  10. Sql触发器模板

    触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程. 触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. SQL Server 2005中触发器可以分 ...