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. Folder Recursion with C#

    by Richard Carr, published at http://www.blackwasp.co.uk/FolderRecursion.aspx Some applications must ...

  2. FindBugs工具常见问题

    1,AM: Creates an empty jar file entry (AM_CREATES_EMPTY_JAR_FILE_ENTRY)/AM: Creates an empty zip fil ...

  3. LoadRunner11.52发布,全新的VTS

    LoadRunner11.52发布,全新的VTShttp://automationqa.com/forum.php?mod=viewthread&tid=2252&fromuid=2 ...

  4. CREATE A LOADING SCENE / SPLASH SCREEN - UNITY

    In the first scene or maybe the Main Menu scene of your game Create an Empty Gameobject. Call it wha ...

  5. [转载]在 JavaScript 中判断“空值”

    http://lync.in/check-empty-value-in-javascript/ 有时候我们会遇到这样的情况:在一些前端控件要提交数据到服务器端的数据验证过程中,需要判断提交的数据是否为 ...

  6. SCRIPT1010: 缺少标识符 常见原因

    SCRIPT1010: 缺少标识符 ,一般是在IE下会出现这个问题. 今天在调试一段js代码时,在chrome,ff下均正常,但是在IE下就是一直这样的提示,出现这个问题的原因主要有以下几点: 1.出 ...

  7. cocos2dx 帧动画的两种创建方式

    看了好几天cocos2dx的帧动画,现在才有点眉目,为了高效期间我们一般会用到 精灵帧缓存(CCSpriteFrameCache) 和动画缓存(CCAnimationCache) .大体的操作步骤: ...

  8. eclipse从数据库逆向生成Hibernate实体类

    做项目必然要先进行数据库表设计,然后根据数据库设计建立实体类(VO),这是理所当然的,但是到公司里做项目后,让我认识到,没有说既进行完数据库设计后还要再“自己”建立一变VO.意思是,在项目设计时,要么 ...

  9. 【Web】Eclipse + Maven + Struts搭建服务器

    一.环境 系统:Windows7 IDE:Eclipse-Kepler Service Release 2 使用插件:Maven(请预先在电脑上安装Maven) 二.搭建 在Eclipse中新建一个M ...

  10. Qt 添加资源文件

    *本人乃小白,博文主要用于个人记录,不保证内容准确无误* 我们编写的gui可能需要一些额外的资源(比如贴图用的图片),可用资源文件统一管理.以下以图片为例. 用qt creator 打开工程,为工程新 ...