CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
题意:给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 (位运算,数论,规律,枚举)的更多相关文章
- 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
C. Amr and Chemistry Problem's Link: http://codeforces.com/problemset/problem/558/C Mean: 给出n个数,让你通过 ...
- Codeforces 558C Amr and Chemistry 暴力 - -
点击打开链接 Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces 558C Amr and Chemistry
题意: n个数.每次能够选一个数 让其 *=2 或者 /=2 问至少操作多少次使得全部数相等. 思路: 对于每一个数,计算出这个数能够变成哪些数,以及变成那个数的最小步数,用两个数组保存 cnt[i] ...
- Codeforces 558C Amr and Chemistry 全都变相等
题意:给定一个数列,每次操作仅仅能将某个数乘以2或者除以2(向下取整). 求最小的操作次数使得全部的数都变为同样值. 比赛的时候最后没实现.唉.之后才A掉.開始一直在想二分次数,可是半天想不出怎 ...
- Codeforces 1208F Bits And Pieces 位运算 + 贪心 + dp
题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向 ...
- CodeForces 165E Compatible Numbers(位运算 + 好题)
wo integers x and y are compatible, if the result of their bitwise "AND" equals zero, that ...
- 在C#中对枚举进行位运算--枚举组合
由于枚举的基础类型类型为基本的数值类型,支持位运算,因此可以使用一个值表示多个枚举的组合,在定义枚举时需要指定枚举数为2的幂指数方便进行位运算,即枚举数为1,2,4,8…,或1,1<<1, ...
- 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 ...
- 【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- Dao泛型设计和反射反型
(1)DAO泛型设计:当二哥或多个类中有类似的方法时,可以将这些累死的方法提出到类中,形式一个泛型父类 (2)反射反型:在泛型父类中获取子类的具体类型的过程,叫反射反型 package cn.itca ...
- PostgreSQL在线安装
背景:CentOS 7 一.在线yum安装 yum -y install https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7 ...
- Scala, Groovy, Clojure, Jython, JRuby and Java ----我们的工作语言
在曾经的一封邮件中,我指出在众多改变中,最明显的一个就是:在java领地上的JVM上使用其它流行的语言的发展变得越来越快.一些老的和新的创建的基于JVM的语言---JRuby 和 Jython ,Ja ...
- Oracle GoldenGate (ogg) 11.2.1.0.20 是最后一个支持oracle db 10g的 ogg版本号
參考原文: Oracle GoldenGate 11.2.1.0.22 Patch Set Availability (Doc ID 1669160.1) 该文章不做翻译,只摘录当中有价值的信息,例如 ...
- ORC 资料Mark
1 OCR开源代码网址汇总 1.1 OCRE(OCR Easy), http://lem.eui.upm.es/ocre.html 1.2 Clara OCR,http://directory.fs ...
- Python 爬虫实例(1)—— 爬取百度图片
爬取百度图片 在Python 2.7上运行 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author: loveNight import jso ...
- Jquery使容器自适应浏览器窗口
一.几个关键点 1:当文档大小改变时可以通过哪个事件来触发? resize([Data], fn) 可传入data供函数fn处理. 示例: $(window).resize(function(){ a ...
- ansible 提示安装sshpass
之前用ansible一直用的root身份.机器之间又早早的做好了ssh信任.所以一直也没有出现什么问题.今天想想自己不能这么浪了,还是用回普通用户吧: 然而马上就遇到了第一个问题,ansible提示安 ...
- 信号处理函数(1)-alarm定时器
定义: unsigned int alarm(unsigned int seconds); 表头文件: #include<unistd.h> 说明: alarm()用来设置信号SI ...
- android.view.animation(3) - LayoutAnimationController 和 GridLayoutAnimationController
前几篇给大家讲述了如何针对某一个控件应用动画,这篇将给大家讲解如何给容器中的控件应用统一动画.即在容器中控件出现时,不必为每个控件添加进入动画,可以在容器中为其添加统一的进入和退出动画. 从上面的示例 ...