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 ...
随机推荐
- 【js】--常用DOM库工具
/* 2014年3月16号 常用DOM工具库*/var DOM={}; DOM.getElesByClass=function (strClassName,context){ if(typeof st ...
- 【Luogu】P1417烹调方案(排序01背包)
题目链接 对食材进行排序,重载运算符代码如下: struct food{ long long a,b,c; bool operator <(const food &a)const{ re ...
- apache kafka参考
apache kafka参考 消息队列分类: 点对点: 消息生产者生产消息发送到queue中,然后消息消费者从queue中取出并且消费消息.这里要注意: 消息被消费以后,queue中不再有存储,所以 ...
- HDU 1423 Greatest Common Increasing Subsequence ——动态规划
好久以前的坑了. 最长公共上升子序列. 没什么好说的,自己太菜了 #include <map> #include <cmath> #include <queue> ...
- BZOJ 1974 [Sdoi2010]auction 代码拍卖会 ——动态规划
把每一位上不递减的数拆成1+11+11111+11111+..... 然后就可以把巨大的N从复杂度中消掉,因为随着长度的增加1...111%p出现了循环节. 然后就是从n个数中选出几个使他们结果为0( ...
- BZOJ3611 [Heoi2014]大工程 【虚树】
题目 国家有一个大工程,要给一个非常大的交通网络里建一些新的通道. 我们这个国家位置非常特殊,可以看成是一个单位边权的树,城市位于顶点上. 在 2 个国家 a,b 之间建一条新通道需要的代价为树上 a ...
- 强大!不懂html也来学网抓(xmlhttp/winhttp+fiddler)
http://club.excelhome.net/thread-1159783-1-1.html 学习html强力推荐此网站:http://www.w3school.com.cn/ ,绝对权威 交流 ...
- EC++学习笔记(四) 设计与声明
条款18:让接口容易被正确使用,不易被误用 必须考虑客户可能做出什么样的错误(防御式编程)std:shared_ptr会自动使用它的"每个指针专属的删除器",消除了"cr ...
- Lumia 1020 诞生:诺基亚拍照技术的一次狂欢
它在手机发展史上留下一长串坚实的脚印,拥趸遍及世界.它从巅峰滑落,但从未放弃向过去致敬的机会. 2002 年,作为世界上第一款内置摄像头拍照手机,诺基亚 7650 的横空出世将手机行业硬生生推上一个新 ...
- javascript 日期处理类库 moment.js