题目链接:http://codeforces.com/problemset/problem/558/C

题意:有n个数,每个数都可以进行两个操作 *2 或者 /2,求最小的操作次数和,使得所有的数都相等;

计算一下时间复杂度可以知道每个数所能达到数的时间复杂度是log(1e5)最终的时间复杂度也就是 nlog(1e5), 所以暴力能过,

刚开始看到的时候一看数据范围就根本不敢暴力了。。。

我们可以把每个数所有变化情况都记录下来,并记录出现的次数,因为只有用1e5的数据量。

先把数据量范围内的所有比a1大的数(a1*2,a1*4。。。)的次数都相对应的加(1,2,。。。)

然后找比a1小的,/2如果遇到奇数,还要接着往上*2.。。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define N 100100
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
using namespace std; int num[N], cnt[N]; void F(int x)
{
int step = , t = x; cnt[x] ++; while(t* < N)///往上*2得到的都是偶数,直接记录;
{
t = t*;
step ++;
num[t] += step;
cnt[t] ++;
} t = x, step = ; while(t > )
{
if(t!= && t%==)///往下/2得到的可能是奇数, 如果是奇数的话继续往上*2;
{
int y = t/;
int step0 = step+;
while(y* < N)
{
y = y*;
step0 ++;
num[y] += step0;
cnt[y] ++;
}
}
t = t/;
step ++;
num[t] += step;
cnt[t] ++;
}
} int main()
{
int n, x;
while(scanf("%d", &n)!=EOF)
{
met(cnt, );
met(num, );
for(int i=; i<n; i++)
{
scanf("%d", &x);
F( x );
}
int ans = INF;
for(int i=; i<N; i++)
{
if(n == cnt[i])
ans = min(ans, num[i]);
}
printf("%d\n", ans);
}
return ;
}

Amr and Chemistry---cf558C(暴力,加技巧)的更多相关文章

  1. CF 305A——Strange Addition——————【暴力加技巧】

    A. Strange Addition time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. zzulioj--1815--easy problem(暴力加技巧)

    1815: easy problem Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 98  Solved: 48 SubmitStatusWeb Bo ...

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

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

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

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

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

  6. CF 558 C. Amr and Chemistry 暴力+二进制

    链接:http://codeforces.com/problemset/problem/558/C C. Amr and Chemistry time limit per test 1 second ...

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

  8. Amr and Chemistry

    C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...

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

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

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

随机推荐

  1. httpclient4.5 的一些细节

    本文转自:http://mercymessi.iteye.com/blog/2250161 httpclient是Apache下的一个用于执行http网络访问的一个工具包. 大致流程:新建一个http ...

  2. Eclipse中复制android项目后要改动的地方

    1.清单文件中,改package=2.修改包名3.清单文件中app_name F3点进去修改名字

  3. Linux网络编程wait()和waitpid()的讲解

    本文讲的是关于wait和waitpid两者的区别与联系.为避免僵尸进程的产生,无论我们什么时候创建子进程时,主进程都需要等待子进程返回,以便对子进程进行清理.为此,我们在服务器程序中添加SIGCHLD ...

  4. resize2fs命令出现这个错误“resize2fs: Operation not permitted While trying to add group #6656” 有数据的会丢数据

    1. resize2fs命令出现这个错误“resize2fs: Operation not permitted While trying to add group #6656”,并且在/var/log ...

  5. 清除Css中select的下拉箭头样式

    select {/*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/border: solid 1px #000; /*很关键:将默认的select选择框样式清除*/appeara ...

  6. golang Time to String

    golang Time to String allenhaozi · 2016-09-02 09:00:00 · 2447 次点击 · 预计阅读时间 1 分钟 · 19分钟之前 开始浏览 这是一个创建 ...

  7. Oracle 错误:ORA-06413: Connection not open 解决办法

    http://blog.csdn.net/neso520/article/details/6037411 ——————————————————————————————————————————————— ...

  8. Mybatis实现了接口绑定,使用更加方便。

    1.Mybatis实现了接口绑定,使用更加方便. 在ibatis2.x中我们需要在DAO的实现类中指定具体对应哪个xml映射文件, 而Mybatis实现了DAO接口与xml映射文件的绑定,自动为我们生 ...

  9. UML概述

    UML (Unified Modeling Language)统一建模语言,是描述.构造和文档化系统制品的可视化语言,是一种图形表示法. UML用途:UML是一种工具,主要用在我们对软件用面向对象的方 ...

  10. css -- 运用@media实现网页自适应中的几个关键分辨率

    经常为不同分辨率设备或不同窗口大小下布局错位而头疼,可以利用@media screen实现网页布局的自适应,但是怎样兼容所有主流设备就成了问题.到底分辨率是多少的时候设置呢? 先看下面的代码,这是从b ...