CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
来源:互联网 发布:单片机毕业设计代做 编辑:程序博客网 时间:2024/05/28 05:16
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 2139062143using 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;}
0 0
- CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
- Codeforces 558C Amr and Chemistry(数论+位运算)
- Codeforces 558C Amr and Chemistry 规律
- Codeforces 558C - Amr and Chemistry (枚举)
- Code Forces 558 C. Amr and Chemistry(数论+位运算)
- codeforces 558C Amr and Chemistry(位操作)
- !codeforces 558C Amr and Chemistry-yy题-(位运算相关)
- codeforces #312C 558C C. Amr and Chemistry(位运算)
- Codeforces 558C Amr and Chemistry 位操作
- codeforces 558 C Amr and Chemistry
- Codeforces 558C Amr and Chemistry
- Codeforces 558C Amr and Chemistry 暴力 - -
- codeforces 558C Amr and Chemistry
- Codeforces 558C Amr and Chemistry
- 【23.39%】【codeforces 558C】Amr and Chemistry
- Codeforces 558C Amr and Chemistry(dp)
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry(数论)
- cf#312-C - Amr and Chemistry-枚举
- Android布局(四)FrameLayout
- 机会留给有准备的人
- windows窗口分析,父窗口,子窗口,所有者窗口
- view的触摸事件
- 二叉树的镜像
- CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
- mysql基础综述(四)
- JavaScript跨域
- C++设计模式详解
- Centos 6.5 final 安装 xtrabackup 2.2.11 小记
- javascript的dom模型
- Num 6 : C语言中IO的格式问题(新手)
- 第三章 垃圾收集器与内存分配策略
- 入门级的随机数生成工具