Codeforces 558C - Amr and Chemistry (枚举)
来源:互联网 发布:你有毒网络用语 编辑:程序博客网 时间:2024/05/16 01:04
枚举每个数乘二和除二过程中所能到达的所有数,并记录到达时的步数,最后统计在所有数都能到达的数字中步数和最小的数字。
因为只有*2和/2的操作,所以往大往小都是log级的。
注意,如果一个数是奇数,那么它本身一直乘二所能到达的数字,和它除2向下取整再一直乘二所能到达的数字是完全不相同的。因为在二进制下除二后相当于左移把末尾的1弄没了,以后再右移时前缀都不一样了。所以在向下除的过程中每次遇到奇数都要处理一下。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define LL long longint n;LL c[200005];int b[200005];int a[100005];void up(int n,int k){ if(!n) return ; while(n<=200000){ b[n]++; c[n]+=k; k++; n<<=1; }}void solve(int n){ int cur=0; LL num=n*2; up(num,1); num=n; cur=0; while(num){ if(num&1){ up(num/2*2,cur+2); } b[num]++; c[num]+=cur; num>>=1; cur++; }}int main(){ scanf("%d",&n); memset(c,0,sizeof(c)); for(int i=0;i<n;i++){ scanf("%d",&a[i]); solve(a[i]); } LL res=1000000000; for(int i=0;i<=200000;i++){ if(b[i]==n){ res=min(res,c[i]); } } printf("%lld\n",res);}
0 0
- 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
- Codeforces 558C Amr and Chemistry
- 【23.39%】【codeforces 558C】Amr and Chemistry
- CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
- cf#312-C - Amr and Chemistry-枚举
- 558C Amr and Chemistry
- Codeforces 558C Amr and Chemistry 位操作
- Codeforces 558C Amr and Chemistry 全都变相等
- Codeforces 558C Amr and Chemistry(dp)
- Codeforces 558C Amr and Chemistry(数论+位运算)
- codeforces 558C Amr and Chemistry(位操作)
- Codeforces 558C. Amr and Chemistry(暴搞)
- 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
- C++ char*,const char*,string的相互转换
- Win7(64位)安装Microsoft SQL Server Management Studio Express[error 29506]
- RGB颜色对照表
- 无边框窗体拖动
- AutoLayout(四)
- Codeforces 558C - Amr and Chemistry (枚举)
- 报错 You need to use a Theme.AppCompat theme (or descendant) with this activity.
- 黑马程序员_Objective-C语言_封装(汇总)
- 关于Ubuntu不启动X,直接进入字符界面
- Java基础之集合
- 使用jQuery开发一个带有密码强度检验的超酷注册页面
- Android中Activity四种启动模式和taskAffinity属性详解
- DNN报错:The compiler failed with error code 1.
- 30天自制操作系统day18&day19