Amr and Chemistry---cf558C(暴力,加技巧)
题目链接: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(暴力,加技巧)的更多相关文章
- 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 ...
随机推荐
- xmapp+netbeans调试
1.netbeans的设置 2.修改php.ini 如下: [XDebug]zend_extension = "D:\dev\xampp\php\ext\php_xdebug.dll&quo ...
- springMVC介绍
http://www.iteye.com/blogs/subjects/springMVC —————————————————————————————————————————————————————— ...
- VSS (Visual Source Safe 2005) 用法详解
VSS用法指南 The usage of VSS (Visual Source Safe 2005) 1. 首先,当然是得安装好Visual Source Safe 2005 你可以在Visual S ...
- php的form中元素name属性相同时的取值问题
php的form中元素name属性相同时的取值问题:修改元素的名称,在名称后面加上 '[]',然后取值时即可得array()数组. 一.以复选框为例: <html> <head> ...
- 显示图片中CDC和HDC问题
(m_Pic.LoadPictureData(pBuffer, nSize));//接作调用函数读pBuffer的jpg数据准备显示 showimage();//显示图片 void Caccess_t ...
- 简单配置Nginx 指向本地端口,并开启SSL
简单配置Nginx 指向本地端口,并开启SSL,如果要开启SSL,必须使用域名去申请SSL key,一般是两个文件,一般是要收费的. # 在/etc/nginx/nginx.conf 的文件中有下面一 ...
- Spring_day02--Spring的aop操作
Spring的aop操作 1 在spring里面进行aop操作,使用aspectj实现 (1)aspectj不是spring一部分,和spring一起使用进行aop操作 (2)Spring2.0以后新 ...
- 剑指 offer set 11 最小的K个数
总结 1. 假如允许修改给定数组, 那么通过快排的子过程, 可以在 o(n) 时间复杂度内得出结果. 2. 对于海量数据和不允许修改的数据, 通过最小堆的方式更好, 通过维持一个大小为 K 的最小堆
- 总结界面框架_UI_Adapter
本人定期更新经典案例及解决方案如有疑问请联系我QQ1822282728 -- 277627117 下面是常用到的ui Demo 安卓三级筛选菜单listview(非常经典) http://dow ...
- 说说M451例程讲解之串口
/**************************************************************************//** * @file main.c * @ve ...