Codeforces 558C

题意:给n个数字,对每一个数字能够进行两种操作:num*2与num/2(向下取整),求:让n个数相等最少须要操作多少次。

分析:

计算每一个数的二进制公共前缀.

枚举法亦可。

/*
*Author : Flint_x
*Created Time : 2015-07-22 12:33:11
*File name : whust2_L.cpp
*/ #include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
#define inf 2139062143
using namespace std;
const double eps(1e-8); typedef long long lint; #define cls(a) memset(a,0,sizeof(a))
#define rise(i,a,b) for(int i = a ; i <= b ; i++)
#define fall(i,a,b) for(int i = a ; i >= b ; i--) const int maxn = 100000 + 5;
int num[maxn];
int temp[maxn];
int odd[maxn],cnt[maxn];
int n; int main(){
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout); while(cin >> n){
for(int i = 1 ; i <= n ; i++){
scanf("%d",&num[i]);
}
cls(cnt);cls(odd);
sort(num+1,num+n+1);
for(int i = 1 ; i <= n ; i++) temp[i] = num[i];
int t = num[1];
for(int i = 1 ; i <= n ; i++){
while(t ^ num[i]){
if (t < num[i]) num[i] >>= 1;
else t >>= 1;
}
}
for(int i = 1 ; i <= n ; i++) num[i] = temp[i];
for(int i = 1 ; i <= n ; i++){
while (num[i] ^ t){
cnt[i]--;
if(num[i] % 2) odd[i] = cnt[i];
num[i] >>= 1;
}
}
lint ans = inf;
for(int i = 0 ; i < 20 ; i++){
lint x = 0;
for(int j = 1 ; j <= n ; j++){
if (odd[j] == 0 || cnt[j] + i <= odd[j]) x += abs(cnt[j] + i);
else x += abs(odd[j]) + abs(odd[j] - (cnt[j] + i)); }
// cout << x << endl;
ans = min(ans,x);
}
cout << ans << endl;
}
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 暴力 - -

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

  3. Codeforces 558C Amr and Chemistry

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

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

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

  5. Codeforces 1208F Bits And Pieces 位运算 + 贪心 + dp

    题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向 ...

  6. CodeForces 165E Compatible Numbers(位运算 + 好题)

    wo integers x and y are compatible, if the result of their bitwise "AND" equals zero, that ...

  7. 在C#中对枚举进行位运算--枚举组合

    由于枚举的基础类型类型为基本的数值类型,支持位运算,因此可以使用一个值表示多个枚举的组合,在定义枚举时需要指定枚举数为2的幂指数方便进行位运算,即枚举数为1,2,4,8…,或1,1<<1, ...

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

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

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

随机推荐

  1. 四种对象生存期和作用域、static 用法总结

    一.四种对象生存期和作用域 栈对象 隐含调用构造函数(程序中没有显式调用) 堆对象 隐含调用构造函数(程序中没有显式调用),要显式释放 全局对象.静态全局对象 全局对象的构造先于main函数 已初始化 ...

  2. centos7 配置PPTP、L2TP、IPSec服务

    首先,推荐跑下面的脚本: https://github.com/BoizZ/PPTP-L2TP-IPSec-VPN-auto-installation-script-for-CentOS-7 这个脚本 ...

  3. C# ManualResetEvent

    文章转载自:C# ManualResetEvent ManualResetEvent被用于在两个或多个线程间进行线程信号发送. 多个线程可以通过调用ManualResetEvent对象的WaitOne ...

  4. 温故而知新 forEach 无法中断(break)的问题

    forEach无法使用break和return来中断,只能使用throw catch来达到中断的效果了. var id = (function(){ // forEach 是无法中断的.除非用这种ha ...

  5. Android7.0 MessageQueue

    Android中的消息处理机制大量依赖于Handler.每一个Handler都有相应的Looper,用于不断地从相应的MessageQueue中取出消息处理. 一直以来,觉得MessageQueue应 ...

  6. Android学习笔记(八)——显示运行进度对话框

    显示运行进度对话框 我们经常有这种经历:运行某一应用程序时.须要等待一会,这时会显示一个进度(Please Wait)对话框,让用户知道操作正在进行. 我们继续在上一篇中的程序中加入代码~ 1.在上一 ...

  7. C语言复杂声明解读简明方法

    //char (*(*x[3])())[5];//x是什么类型的变量? // //分析C语言声明,关键是搞清楚这个变量是个什么东西(函数.指针.数组), //是函数那么剩下的就是他的参数和返回值, / ...

  8. [docker]存储驱动overlay和overlay2的区别

    overlay和overlay2的区别 参考:http://blog.csdn.net/styshoo/article/details/60715942 docker pull ubuntu 本质区别 ...

  9. 单页应用SPA做SEO的一种清奇的方案

    单页应用SPA做SEO的一种清奇的方案 网上有好几种单页应用转seo的方案,有服务端渲染ssr.有预渲染prerender.google抓AJAX.静态化...这些方案都各有优劣,开发者可以根据不同的 ...

  10. 推荐一个入门最佳Git教程

    这是我最近发现的一个针对入门Git教程,浅显易懂,点到为止,很适合初学者及使用Git的爱好者,学完该教程应付开发工作绰绰有余. http://www.liaoxuefeng.com/wiki/0013 ...