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. 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  2. .net core +codefirst(.net core 基础入门,适合这方面的小白阅读) 【我们一起写框架】领域驱动设计的CodeFirst框架(一)—序篇

    .net core +codefirst(.net core 基础入门,适合这方面的小白阅读)   前言 .net core mvc和 .net mvc开发很相似,比如 视图-模型-控制器结构.所以. ...

  3. 使用sqlplus连接提示:ORA-28002: the password will expire within 7 days

    今天在使用sqlplus时出现,或使用数据库的时候出现类似问题 =============================================== ERROR:ORA-28002: the ...

  4. EAST 自然场景文本检测

           自然场景文本检测是图像处理的核心模块,也是一直想要接触的一个方面. 刚好看到国内的旷视今年在CVPR2017的一篇文章:EAST: An Efficient and Accurate S ...

  5. int main() 与 int _tmain()

    用过C的人都知道每一个C的程序都会有一个main(),但有时看别人写的程序发现主函数不是int main(),而是int _tmain(),而且头文件也不是<iostream.h>而是&l ...

  6. 1 - Reverse Integer

    Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321Discuss: 1. ...

  7. Qt 2D绘图高级篇

    1.拖动模式 在QGraphicView中提供了三种拖动模式,分别是: QGraphicsView::NoDrag :忽略鼠标事件,不可以拖动. QGraphicsView::ScrollHandDr ...

  8. Atitit.spring体系结构大总结

    Atitit.spring体系结构大总结 1. Srping mvc 1 2. Ioc 4 3. ApplicationContext在BeanFactory的基础上构建,区别 4 4. Aop 5 ...

  9. uint64, sizet_t, ssizet_t

    uint64 在32位平台 typedef unsigned long long int uint64_t;在64位平台 typedef unsigned long int uint64_t;不同的t ...

  10. shell脚本中执行mysql命令

    1.mysql -hhostname -uuser -ppsword -e "mysql_cmd" 2. mysql -hhostname -uuser -ppsword < ...