Codeforces 558C Amr and Chemistry
题意:
n个数。每次能够选一个数 让其 *=2 或者 /=2
问至少操作多少次使得全部数相等。
思路:
对于每一个数,计算出这个数能够变成哪些数,以及变成那个数的最小步数,用两个数组保存
cnt[i] 表示序列中有cnt个数能够变成i
step[i] 表示能变成i的 那些数 变成i的花费和是多少。
当中。遇到奇数的时候要特殊处理一下:
比方,7 能够通过 /2 然后 *2得到6,也就是说不论什么奇数 i (不包含1)都能够通过2次操作变为 i -1;
代码例如以下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long ll;
const int N = 1e5+10;
const int INF = 1e9 + 10;
int n;
int a[N], cnt[N];//个数
ll step[N];//总步数 void up(int x, int now)
{
while(x < N)
{
cnt[x]++;
step[x] += now;
now++;
x <<= 1;
}
} void go(int u)
{
up(u, 0);
int now = 1;
while(u)
{
if((u>1) && (u&1))
{
u >>= 1;
up(u, now);
}
else
{
u >>= 1;
cnt[u]++;
step[u] += now;
}
now++;
}
} int main()
{
while(~scanf("%d", &n))
{
memset(cnt, 0, sizeof(cnt));
memset(step, 0, sizeof(step));
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
go(a[i]);
}
ll ans = step[1];
for(int i = 2; i < N; i++)
{
if(cnt[i] == n)
ans = min(ans, step[i]);
}
printf("%I64d\n", ans);
}
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 (位运算,数论,规律,枚举)
Codeforces 558C 题意:给n个数字,对每一个数字能够进行两种操作:num*2与num/2(向下取整),求:让n个数相等最少须要操作多少次. 分析: 计算每一个数的二进制公共前缀. 枚举法 ...
- Codeforces 558C Amr and Chemistry 全都变相等
题意:给定一个数列,每次操作仅仅能将某个数乘以2或者除以2(向下取整). 求最小的操作次数使得全部的数都变为同样值. 比赛的时候最后没实现.唉.之后才A掉.開始一直在想二分次数,可是半天想不出怎 ...
- 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 ...
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry 暴力
C. Amr and Chemistry Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/558/ ...
- Codeforces Round #312 (Div. 2) C.Amr and Chemistry
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experime ...
- C. Amr and Chemistry(Codeforces Round #312 (Div. 2) 二进制+暴力)
C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
随机推荐
- Webstrom卡顿问题解决
1.设置node_modules 打开项目,新建node_modules空文件夹,然后右击选择Mark Directory as,选择Excluded. 2.设置ingore文件 files-> ...
- Android单个按钮自定义Dialog
代码改变世界 Android单个按钮自定义Dialog dialog_layout.xml <?xml version="1.0" encoding="utf-8& ...
- 【Luogu】P3380树套树模板(线段树套Splay)
题目链接 幸甚至哉,歌以咏志. 拿下了曾经是那么遥不可及的线段树,学会了曾经高不可攀的平衡树,弄懂了装B的时候才挂在嘴边的树套树. 每道模板都是链上的一颗珠子.把它们挨个串起来,就成为我成长的历程. ...
- POJ 1038 Bugs Integrated, Inc. ——状压DP
状态压缩一下当前各格子以及上面总共放了几块,只有012三种情况,直接三进制保存即可. 然后转移的时候用搜索找出所有的状态进行转移. #include <map> #include < ...
- [BZOJ3339] Rmq Problem(线段树)
传送门 这个题的方法好像很多啊 1.莫队暴力 2.线段树 + 离线处理 先预处理出sg[i]表示前i个数的sg值,next[i]表示i的下一位置在哪里,如果后面再没有i,那么next[i] = n + ...
- HDU 1166 敌兵布阵【分块】
Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任 ...
- Elasticsearch 禁止Body覆盖URL中的参数
以通过设置参数rest.action.multi.allow_explicit_index为false来关闭覆盖功能. 这个设置会对所有的节点起作用,设置方法如下: 在config/elasticse ...
- 【shell】shell编程(四)-循环语句
上篇我们学习了shell中条件选择语句的用法.接下来本篇就来学习循环语句.在shell中,循环是通过for, while, until命令来实现的.下面就分别来看看吧. for for循环有两种形式: ...
- 标准C程序设计七---11
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- CKeditor如何实现图片上传功能
http://makaiyuan.blog.51cto.com/5819595/1049521 如何在数据库中导入excel文件内的数据:http://jingyan.baidu.com/album/ ...