题目链接: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. Git中保存用户名和密码

    每次操作都需要输入用户名和密码感觉很繁琐,解决方法,在本地的工程文件夹的.git下打开config文件添加: [credential]     helper = store 再输入一次用户名密码后就可 ...

  2. excel鼠标拖选慢shift选择快的问题

    今天遇到个惊天大坑,关于excel的,最近,一直在调查这个东西,刚开始真的是毫无头绪,反正现在就是excel的值的copy会偶尔慢,慢的情况也是不明白,就是稀里糊涂的调查. 刚开始连100%再现这个b ...

  3. sort-uniq-cut-join命令练习

    [root@linux Desktop]# cat> fruits.txt banana orange persimmon %%banana apple ORAGE cat> fruits ...

  4. CSS布局奇淫技巧之--各种居中<转>

    居中是我们使用css来布局时常遇到的情况.使用css来进行居中时,有时一个属性就能搞定,有时则需要一定的技巧才能兼容到所有浏览器,本文就居中的一些常用方法做个简单的介绍. 注:本文所讲方法除了特别说明 ...

  5. [转]SOA接口的两种常用实现比较:SOAP vs REST

    原文链接:http://blog.csdn.net/zhaohuabing/article/details/39643127 SOA架构用于异构系统的协作,因此需要一种跨操作系统.跨语言的通用的消息交 ...

  6. digitalocean --- How To Install Apache Tomcat 8 on Ubuntu 16.04

    https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-ubuntu-16-04 Intr ...

  7. hdu 1421:搬寝室(动态规划 DP + 排序)

    搬寝室 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  8. Windows下安装Apache 2.2.21图文教程

    https://www.jb51.net/article/52086.htm 本文详细介绍了在Windows平台上安装Apache的过程,希望对初次安装Apache的朋友有所帮助. 1. 软件准备 我 ...

  9. iOS开发之--实现倒计时显示时分秒

    这段时间写公司的一个外包项目,需要用到倒计时:需要显示时分秒,通过在网上搜集资料,找到了2中方法,我把这两种方法结合起来,可以很好的满足这个需求: 1.创建一个类继承自UIlabel,用来展示时分秒的 ...

  10. ionic触摸事件

    官方文档:http://ionicframework.com/docs/api/directive/onHold/ on-hold 长按事件on-tap 点击事件 on-double-tap  双击事 ...