Amr and Chemistry---cf558C(暴力,加技巧)
题目链接:http://codeforces.com/problemset/problem/558/C
题意:有n个数,每个数都可以进行两个操作 *2 或者 /2,求最小的操作次数和,使得所有的数都相等;
计算一下时间复杂度可以知道每个数所能达到数的时间复杂度是log(1e5)最终的时间复杂度也就是 nlog(1e5), 所以暴力能过,
刚开始看到的时候一看数据范围就根本不敢暴力了。。。
我们可以把每个数所有变化情况都记录下来,并记录出现的次数,因为只有用1
e5的数据量。
先把数据量范围内的所有比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(暴力,加技巧)的更多相关文章
- CF 305A——Strange Addition——————【暴力加技巧】
A. Strange Addition time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- zzulioj--1815--easy problem(暴力加技巧)
1815: easy problem Time Limit: 1 Sec Memory Limit: 128 MB Submit: 98 Solved: 48 SubmitStatusWeb Bo ...
- 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
C. Amr and Chemistry Problem's Link: http://codeforces.com/problemset/problem/558/C Mean: 给出n个数,让你通过 ...
- 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 558C Amr and Chemistry 暴力 - -
点击打开链接 Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input stan ...
- CF 558 C. Amr and Chemistry 暴力+二进制
链接:http://codeforces.com/problemset/problem/558/C C. Amr and Chemistry time limit per test 1 second ...
- 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 ...
- Amr and Chemistry
C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #312 (Div. 2) C.Amr and Chemistry
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experime ...
- 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 ...
随机推荐
- linux超级终端minicom的使用方法
===== 一.Minicom介绍 ===== Linux下的Minicom的功能与Windows下的超级终端功能相似,可以通过串口控制外部的硬件 设备.适于在linux通过超级终端对 ...
- CSS学习笔记(12)--Flex 布局教程:实例篇
原文--阮一峰博客 作者: 阮一峰 日期: 2015年7月14日 上一篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法. 你会看到,不管是什么布局,Flex往往都可以几行命令搞定. 我只 ...
- centos7系统nginx下phalcon环境搭建
之前我们采用的是Apache服务器,可是每秒响应只能达到2000,听说nginx可以轻易破万, 于是换成nginx试试. phalcon的官网有nginx重写规则的示例,可是却与apache的不一致, ...
- lua工具库penlight--04路径和目录
使用路径 程序不应该依赖于奇葩的系统,这样你的代码会难以阅读和移植.最糟糕的是硬编码的路径, windows和Unix的路径分隔符正好相反.最好使用path.join,它可以帮助你解决这个问题. pl ...
- C语言 · 利息计算
算法提高 利息计算 时间限制:1.0s 内存限制:512.0MB 编制程序完成下述任务:接受两个数,一个为用户一年期定期存款金额,一个为按照百分比格式表示的利率:程序计算一年期满 后 ...
- LAMP 环境搭建关键步骤及注意事项
一.安装MySQL1): 编译安装MySQL+----------------------------------------------------------------------------- ...
- WebApi(6) 后台C#调用WebApi
https://www.cnblogs.com/cxd1008/p/6640015.html 今天来写一下后台C#代码如何访问webapi 这里使用HttpClient方法访问webapi也是很常用的 ...
- public, protected, private,internal,protected internal的区别
虽然这个知识比较简单, 但是老是会忘, 写上来, 增强记忆. 在C#语言中,共有五种访问修饰符:public.private.protected.internal.protected internal ...
- hdu4122(单调队列)
处理题目中给的日期,然后用单调队列维护 Alice's mooncake shop Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32 ...
- 【BZOJ4883】[Lydsy2017年5月月赛]棋盘上的守卫 KM算法
[BZOJ4883][Lydsy2017年5月月赛]棋盘上的守卫 Description 在一个n*m的棋盘上要放置若干个守卫.对于n行来说,每行必须恰好放置一个横向守卫:同理对于m列来说,每列 必须 ...