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 ...
随机推荐
- hibernate 在做更新和删除的时候一定要把事务开启
在做更新和删除的时候一定要把事务开启 在做更新和删除的时候一定要把事务开启 在做更新和删除的时候一定要把事务开启 重要的事情说三遍!!! curd之前配置文件 <property name=&q ...
- 【转帖】Dubbo:来自于阿里巴巴的分布式服务框架
http://www.biaodianfu.com/dubbo.html Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被 ...
- Linux 间网线直连
核心提示:两台linux 系统用交叉线直连的方法: 1.交叉网线制作 A头:白橙,橙,白绿,蓝,白蓝,绿,白褐,褐 B头:白绿,绿,白橙,蓝,白蓝,橙,白褐,褐 即 B头的1.3脚相对A头互换:2脚与 ...
- 变动性算法源代码分析与使用示例(copy_backward、 transform、 replace_copy_if 等)
首先回顾前面的文章,我们把for_each 归类为非变动性算法,实际上它也可以算是变动性算法,取决于传入的第三个参数,即函数 指针.如果在函数内对容器元素做了修改,那么就属于变动性算法. 变动性算法源 ...
- Python sql注入 过滤字符串的非法字符
#coding:utf8 #在开发过程中,要对前端传过来的数据进行验证,防止sql注入攻击,其中的一个方案就是过滤用户传过来的非法的字符 def sql_filter(sql, max_length= ...
- Python强制抛出自定义异常
raise Exception("My Exception") 当程序运行到这行时,会抛出异常,打印出Exception: My Exception
- atitit。gui 界面皮肤以及换肤总结 java .net c++
atitit.gui 界面皮肤以及换肤总结 java .net c++ 1. Swing 的皮肤 1 1.1. windows风格 1 1.2. Mac风格 ( liquid 框架) 1 2. 如何给 ...
- [elk]Mutate filter plugin增删改查字段
Mutate filter plugin参考: https://www.elastic.co/guide/en/logstash/current/plugins-filters-mutate.html ...
- 响应式布局框架 Pure-CSS 5.0 示例中文版-上
0. Pure-CSS 介绍 Pure CSS 是雅虎出品的 CSS 框架, 依托于Normalize.CSS,在不适用任何JS代码情况下即可实现响应式布局的轻量级框架,无依赖,体积小. 1. CDN ...
- NYOJ 467 中缀式变后缀式
做了表达式求值那道题之后做的 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 人们的日常习惯是把算术表达式写成中缀式,但对于机器来说更"习惯于"后 ...