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 ...
随机推荐
- 如何处理JSON中的特殊字符 介绍几种解决方案
http://www.nowamagic.net/librarys/veda/detail/216JSON 是适用于 Ajax 应用程序的一种有效格式,原因是它使 JavaScript 对象和字符串值 ...
- Linux命令-文件搜索命令:locate
实际上是在分区表上搜索文件,比find命令速度更快 locate inittab 查找名称包含inittab的所有信息(快速搜索,实际上它是搜索linux资料库,区别于find在某一个磁盘分区或者某一 ...
- HDU 5186 zhx's submissions (进制转换)
Problem Description As one of the most powerful brushes, zhx submits a lot of code on many oj and mo ...
- python 中 numpy array 中的维度
简介 numpy 创建的数组都有一个shape属性,它是一个元组,返回各个维度的维数.有时候我们可能需要知道某一维的特定维数. 二维情况 >>> import numpy as np ...
- Decoration2:引入Angularjs显示前台一条数据
SpringMVC内置的RestFul API格式采用的是最复杂最全面的HATEOAS规范,对于简单应用来说,前台解析起来不方便,我们下面主要想办法重新定义一种简单的RestFulAPI. (1)先是 ...
- HTTP协议 - 协议格式
HTTP 是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和 扩展.目前在WWW中使用的是HTTP/1.0 ...
- 419. Roman to Integer【medium】
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- Windows Phone 推送通知的第四类推送
在 MSDN 文档有关 Windows Phone 推送通知 有关推送的内容包含 Tile.Toast.Raw 这三种通知.这三种通知 的方式类似,运用的场合不同,这里不再赘述,它们的运行原理类似: ...
- zookeeper是如何选取主leader的?
以一个简单的例子来说明整个选举的过程.假设有五台服务器组成的zookeeper集群,它们的id从1-5,同时它们都是最新启动的,也就是没有历史数据,在存放数据量这一点上,都是一样的.假设这些服务器依序 ...
- jquery 悬浮验证框架 jQuery Validation Engine
中文api 地址 http://code.ciaoca.com/jquery/validation-engine/ 和bootstarp 一起使用不会像easyui 验证那样生硬 修改版 原版 ...