Codeforces 558C Amr and Chemistry
题意:
n个数。每次能够选一个数 让其 *=2 或者 /=2
问至少操作多少次使得全部数相等。
思路:
对于每一个数,计算出这个数能够变成哪些数,以及变成那个数的最小步数,用两个数组保存
cnt[i] 表示序列中有cnt个数能够变成i
step[i] 表示能变成i的 那些数 变成i的花费和是多少。
当中。遇到奇数的时候要特殊处理一下:
比方,7 能够通过 /2 然后 *2得到6,也就是说不论什么奇数 i (不包含1)都能够通过2次操作变为 i -1;
代码例如以下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long ll;
const int N = 1e5+10;
const int INF = 1e9 + 10;
int n;
int a[N], cnt[N];//个数
ll step[N];//总步数 void up(int x, int now)
{
while(x < N)
{
cnt[x]++;
step[x] += now;
now++;
x <<= 1;
}
} void go(int u)
{
up(u, 0);
int now = 1;
while(u)
{
if((u>1) && (u&1))
{
u >>= 1;
up(u, now);
}
else
{
u >>= 1;
cnt[u]++;
step[u] += now;
}
now++;
}
} int main()
{
while(~scanf("%d", &n))
{
memset(cnt, 0, sizeof(cnt));
memset(step, 0, sizeof(step));
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
go(a[i]);
}
ll ans = step[1];
for(int i = 2; i < N; i++)
{
if(cnt[i] == n)
ans = min(ans, step[i]);
}
printf("%I64d\n", ans);
}
return 0;
}
Codeforces 558C Amr and Chemistry的更多相关文章
- 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
C. Amr and Chemistry Problem's Link: http://codeforces.com/problemset/problem/558/C Mean: 给出n个数,让你通过 ...
- Codeforces 558C Amr and Chemistry 暴力 - -
点击打开链接 Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input stan ...
- CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
Codeforces 558C 题意:给n个数字,对每一个数字能够进行两种操作:num*2与num/2(向下取整),求:让n个数相等最少须要操作多少次. 分析: 计算每一个数的二进制公共前缀. 枚举法 ...
- Codeforces 558C Amr and Chemistry 全都变相等
题意:给定一个数列,每次操作仅仅能将某个数乘以2或者除以2(向下取整). 求最小的操作次数使得全部的数都变为同样值. 比赛的时候最后没实现.唉.之后才A掉.開始一直在想二分次数,可是半天想不出怎 ...
- 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 ...
- 【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 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 Round #312 (Div. 2) C.Amr and Chemistry
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experime ...
- 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 ...
随机推荐
- vmware安装CentOS " Intel VT-x 处于禁用状态"
我刚用虚拟机vmware 11安装CentOS 7 ,出现错误“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的问题,如下图. Intel VT-x 即Virtualiza ...
- xtu数据结构 G. Count the Colors
G. Count the Colors Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Jav ...
- [android 应用框架api篇] bluetooth
bluetooth接口 android.bluetooth.jar 官网网址: 接口类: https://developer.android.com/reference/android/bluetoo ...
- 【JavaScript 12—应用总结】:弹出登录框
导读:上篇博客中,做好了个人中心的下拉菜单,这次,将做每个网站都会有的一个登录功能,以此类推,可以做出别的想要的弹出框,如错误提示啦,或者注册. 一.实现分析 首先:和下拉菜单一样,需要通过CSS样式 ...
- asp.net提交危险字符处理方法之一
在form表单提交前,可以在web页面,submit按钮的click事件中,使用js函数对,可能有危险字符的内容进行编码. 有3个函数可用: encodeURI() 函数可把字符串作为 URI 进行编 ...
- 用ajax异步请求一个块
使用用户控件,作为一个块,生成一段独立的html. 请求一个一般应用程序,在一般应用程序中加载读取用户控件,并给用户控件传值. Page page = new Page(); ...
- 划分树C++版百度百科模板
此代码同hdu2665改编#include <iostream> #include <cstdio> #include<cstring> #include < ...
- 【Luogu】P2530化工厂装箱员(DP)
题目链接 不知道做出这道题是我能力的一个提升还是能力的回归. DP.设f[i][j][k][l]是已经取了i个产品,现在手里还拿着j件A,k件B,l件C,最小的操作数. 然后状转方程乱搞啊 #incl ...
- 【Luogu】P2258子矩阵(状态压缩,DP)
233今天蒟蒻我连文化课都没听光想着这个了 然后我调了一下午终于过了!!! 一看数据范围似乎是状压,然而216等于65536.开一个65536*65536的二维数组似乎不太现实. 所以Rqy在四月还是 ...
- Opencv学习笔记——视频高斯模糊并分别输出
用两个窗口进行对比 #include "stdafx.h" #include <iostream> #include <opencv2/core/core.hpp ...