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. [Aaronyang] 写给自己的WPF4.5 笔记18[几何图形*Geometry图文并茂讲解]

    为什么要掌握?因为WPF 3D知识很多与它Geometry对比,所以我要系统学一下. --学会用Geometry给Path的Data属性填充. 图形可以转换成路径,Path的值,当然你也可以直接使用R ...

  2. velocity 显示List和Map方法

    一.遍历个map类型 1.先看后台java程序Java代码     Map<String,String> paramValues=new HashMap<String, String ...

  3. 菜鸟译文(三)——JDK6和JDK7中substring()方法的对比

    substring(int beginIndex, int endIndex)方法在JDK6和JDK7中是不同的.了解他们的区别可以让我们更好的使用这个方法.方便起见,以下用substring() 代 ...

  4. Cocos2dx使用wxsqlite开源加密SQLite3数据库

    最近使用wxsqlite加密sqlite3数据库,刚开始折腾好几天,在xcode上一直编译不通过,后来在sqlite3.c找到配置,编译顺利通过,太激动了,哈哈,废话少说!总结一下android和io ...

  5. [原创]android使用代码生成LayerDrawable的方法和注意事项

    为了有更好的UI体验,一般我们会把button.textview等控件的背景设置上阴影.传统的做法是美工提供一张具有阴影效果的nine patch图,然后将其在xml文件中添加到background属 ...

  6. 64位Linux下编译搭建Nginx1.5与PHP5.5(CentOS6.4)

    (1)安装Nginx1.5.2更新Nginx和PHP的依赖包yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng \libp ...

  7. Spring3系列2 -- 松耦合的实现

    Spring3系列2 -- 松耦合的实现 一.      环境 spring-framework-3.2.4.RELEASE jdk1.7.0_11 Maven3.0.5 eclipse-jee-ju ...

  8. (笔记)Linux内核学习(四)之系统调用

    一 用户空间和内核空间 Linux内核将这4G字节虚拟地址空间的空间分为两部分: l  将最高的1G字节(从虚拟地址0xC0000000到0xFFFFFFFF),供内核使用,称为“内核空间”. l  ...

  9. Xcode报错:“Your build settings specify a provisioning profile with the UUID..... however, no such provisioning profile was found”

    运行环境: Xcode5 & 5.0及以上版本 对工程进行Archive打包的时候出现如下错误   问题描述: Code Sign error: No matching provisionin ...

  10. [PaPaPa][需求说明书][V0.1]

    PaPaPa软件需求说明书V0.1 前   言 我觉得我们废话不能多,废话一多的话大家就找不到重点了,其实本项目是出于技术研究的目的来开发的,大家讨论了半天决定要做社(yue)交(pao)类的项目. ...