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 Ubuntu 打开.exe文件
这两天在编译Android源码,进行到要在Linux里安装烧录软件那一步,要先装驱动,故了解了如何在linux下打开.exe文件. .exe 文件在linux下不能直接打开,可有两种方式打开:. 1. ...
- python模块之XlsxWriter 详解
Xlsx是python用来构造xlsx文件的模块,可以向excel2007+中写text,numbers,formulas 公式以及hyperlinks超链接. 可以完成xlsx文件的自动化构造,包括 ...
- 对java中arraylist深入理解
1.ArrayList插入删除一定慢么? 取决于你删除的元素离数组末端有多远,ArrayList拿来作为堆栈来用还是挺合适的,push和pop操作完全不涉及数据移动操作. 2.ArrayList的遍历 ...
- Unix系统编程()信号处理器简介
信号处理器简介 信号处理器程序(也称为信号捕捉器)是当指定信号传递给进程时将会调用的一个函数.这里会学习信号处理器的基本原理,后面将继续学习. 调用信号处理器程序,可能会随时打断主程序流程:内核代表进 ...
- .bss,.data,.text,.rodata
那天工作时候发现build的时候发现问题, 问题内容是:.text的空间太小了. 我一直以为写代码,就真是弄懂代码怎么写,式样书怎么写,或者弄懂代码的问题所在, 没有想到在build的时候出现问题.结 ...
- 在其模块列表中有一个错误模块“ManagedPipelineHandler”。
C:\Windows\Microsoft.NET\Framework\v4.0.30319 命令行: aspnet_regiis -i
- Socket的三个关联函数
/*lrs_save_param将静态或接收到的缓冲区保存到参数中*/lrs_save_param (char *s_desc, char *buf_desc, char *param_name, i ...
- MySQL--执行mysql脚本及其脚本编写
http://www.cnblogs.com/kex1n/archive/2010/03/26/2286504.html
- layui多选框
多选下拉框:http://sun.faysunshine.com/layui/formSelects-v4/example/example_v4.html 1.下载formSelects-v4.1 2 ...
- Hibernate_day01--课程安排_Hibernate概述_Hibernate入门
Hibernate_day01 Hibernate课程安排 今天内容介绍 WEB内容回顾 JavaEE三层结构 MVC思想 Hibernate概述 什么是框架 什么是hibernate框架(重点) 什 ...