题意:

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的更多相关文章

  1. 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry

    C. Amr and Chemistry Problem's Link: http://codeforces.com/problemset/problem/558/C Mean: 给出n个数,让你通过 ...

  2. Codeforces 558C Amr and Chemistry 暴力 - -

    点击打开链接 Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input stan ...

  3. CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)

    Codeforces 558C 题意:给n个数字,对每一个数字能够进行两种操作:num*2与num/2(向下取整),求:让n个数相等最少须要操作多少次. 分析: 计算每一个数的二进制公共前缀. 枚举法 ...

  4. Codeforces 558C Amr and Chemistry 全都变相等

     题意:给定一个数列,每次操作仅仅能将某个数乘以2或者除以2(向下取整). 求最小的操作次数使得全部的数都变为同样值. 比赛的时候最后没实现.唉.之后才A掉.開始一直在想二分次数,可是半天想不出怎 ...

  5. 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 ...

  6. 【23.39%】【codeforces 558C】Amr and Chemistry

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 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/ ...

  8. Codeforces Round #312 (Div. 2) C.Amr and Chemistry

    Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experime ...

  9. 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 ...

随机推荐

  1. PHP cannoy modify header information - headers already sent by ....

    我采用的是MVC模式的写法,代码和html分离的写法 <?php require '../mysql_connect.php'; require('../model/functions.php' ...

  2. 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))

    这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

  3. shell-001

    for: shell_test #!/bin/bash var= var=$var+ echo $var mkdir only_a_joke shell_joke #!/bin/bash ./shel ...

  4. nginx 的日志切割

    nginx的日志切割脚本 说明:在nginx的配置文件中nginx.conf并没有定义access_log的位置, 在/usr/local/nginx/conf/vhost/下的配置文件中定义了acc ...

  5. xcode错误-第三方的东西他不支持

    ld:' /用户/ tanqihong /桌面/金粒子公司/金粒子公司/ Carloans / Carloans /第三/ TongLianPay / lib_release / libAPayLib ...

  6. SPOJ 375 Query on a tree【树链剖分】

    题目大意:给你一棵树,有两个操作1.修改一条边的值,2.询问从x到y路径上边的最大值 思路:如果树退化成一条链的话线段树就很明显了,然后这题就是套了个树连剖分,调了很久终于调出来第一个模板了 #inc ...

  7. BZOJ 1191: [HNOI2006]超级英雄Hero【二分图匹配】

    裸的匹配题,一眼就能看出来二分图的模型,是某个经典题的改编.貌似某本图论书上讲过的,有N个人以及M个职位,每个职位只能提供给一个人,而每个人由于能力有限只能胜任有限个职位,问是否有办法使得每个人都有工 ...

  8. sqlite-jdbc

    sqlite-jdbc驱动下载 https://bitbucket.org/xerial/sqlite-jdbc/downloads import java.sql.*; public class T ...

  9. 网页抓取小工具(IE法)

    网页抓取小工具(IE法)—— 吴姐 http://club.excelhome.net/thread-1095707-1-1.html 用IE提取网页资料的好处在于:所见即所得,网页上能看到的信息一般 ...

  10. 【leetcode】lower_bound

    int binary_search(const vector<int>& stones,int val){ int sz=stones.size(); ,r=sz-; while( ...