(CodeForces 558C) CodeForces 558C
题目链接:http://codeforces.com/problemset/problem/558/C
题意:给出n个数,让你通过下面两种操作,把它们转换为同一个数。求最少的操作数。
1.ai = ai*2
2.ai = ai/2,向下取整
思路:
可以除以二 或者 乘以二,就相当于位运算的右移和左移。用两个数组,vis 数组, cnt 数组。刚开始都初始化为0; vis[i] 表示 i 这个数可以由几个数转化而来,cnt[i] 表示题目给出的 n 个数全部转化为 i 需要的操作数。
首先遍历数组找到 ai 的最大值记为 MAX,那么所有数转化的上界就是 MAX,因为如果最终转化的数如果大于MAX,那么所有值都要转化为大于MAX的那个数,很明显这不是最后的答案。
把一个数表示为二进制数,
1、如果最低位是0,那么这个数右移一位(除以2),再左移一位(乘以2),就得到原来的数
2、如果最低位是1,那么这个数右移一位(除以2),再左移一位(乘以2),得不到原来的数
那么:
3要转化为8,最少需要4步操作,先除以2,再乘以2,再乘以2,再乘以2
2要转化为8,最少需要2步操作,先乘以2,再乘以2
处理一个数 ai:
1、对 ai 执行左移操作,记录 ai 通过左移能够得到的数字,上限为MAX,vis 数组加1,cnt数组记录步数
2、对 ai 执行右移操作,直到 ai 为0
若 ai 的最低位为1,右移一步之后,进行左移,上限为MAX,维持vis数组和cnt数组
若 ai 的最低位为0,右移一步,维持vis数组和cnt数组
这样我们就把一个数的所有情况都处理出来了,并且是最少的操作数。
最后遍历数组找 vis[i] 为n,并且操作数最小。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a))
#define ll long long
#define N 100010
int m,vis[N*];///有多少数通过乘以2或除以2能到i
int te[N*];///统计到i需要多少步
int a[N*];
void add(int s)
{
vis[s]++;
int a=s,b=s;
int h=,k=;
while(a<=m)///往上乘不能超过最大值
{
a=a<<;
h++;
vis[a]++;
te[a]+=h;
}
while(b)
{
if(b& && b!=)///如果二进制末尾是1除以2再乘以2不是原来的数
{
b=b>>;
k++;
vis[b]++;
te[b]+=k;
int k1=k;
int d=b;
while(d<=m)
{
d=d<<;
k1++;
vis[d]++;
te[d]+=k1;
}
}
else
{
b=b>>;
k++;
vis[b]++;
te[b]+=k;
}
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
m=max(m,a[i]);
}
met(vis,);met(te,);
for(int i=;i<n;i++)
add(a[i]);
int ans=INF;
for(int i=;i<N*;i++)
{
if(vis[i]==n)///需要所有的数都能到i
ans=min(ans,te[i]);///取最小步数
}
printf("%d\n",ans);
return ;
}
(CodeForces 558C) CodeForces 558C的更多相关文章
- (水题)Codeforces - 4C - Registration system
https://codeforces.com/problemset/problem/4/C 用来哈希的一道题目,用map也可以强行过,但是性能慢了6倍,说明是在字符串比较的时候花费了接近6倍的时间. ...
- (水题)Codeforces - 327C - Magic Five
https://codeforces.com/problemset/problem/327/C 因为答案可以有前导零,所以0和5一视同仁.每个小节内,以排在第 $i$ 个的5为结尾的序列即为在前面 $ ...
- (水题)Codeforces - 650A - Watchmen
http://codeforces.com/contest/650/problem/A 一开始想了很久都没有考虑到重复点的影响,解欧拉距离和曼哈顿距离相等可以得到 $x_i=x_j$ 或 $y_i=y ...
- (水题)Codeforces - 630H - Benches
https://codeforces.com/problemset/problem/630/H 又一个组合数学的问题,我们先考虑在 $n$ 列中选出 $5$ 列来放椅子,然后对第一列有 $n$ 种放法 ...
- 网络流(费用流)CodeForces 321B:Ciel and Duel
Fox Ciel is playing a card game with her friend Jiro. Jiro has n cards, each one has two attributes: ...
- 网络流(最大流)CodeForces 512C:Fox And Dinner
Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). T ...
- 网络流(最大流) CodeForces 546E:Soldier and Traveling
In the country there are n cities and m bidirectional roads between them. Each city has an army. Arm ...
- 点分治 (等级排) codeforces 321C
Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has n cities connected ...
- Codeforces 746D:Green and Black Tea(乱搞)
http://codeforces.com/contest/746/problem/D 题意:有n杯茶,a杯绿茶,b杯红茶,问怎么摆放才可以让不超过k杯茶连续摆放,如果不能就输出NO. 思路:首先,设 ...
随机推荐
- .NET MVC4 ApiController拦截器的使用
目前在公司新开发了一个项目,第一次正式使用.NET MVC4来搭建,用拦截器来处理权限验证. 自定义拦截器需继承ActionFilterAttribute类,重写OnActionExecuting和O ...
- 剑指OFFER之把数组排成最小的数(九度OJ1504)
题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 输入: 输 ...
- JNI-使用RegisterNatives注册本地方法
转自: http://blog.chinaunix.net/uid-26009923-id-3410141.html 1. 以前在jni中写本地方法时,都会写成 Java_com_example_he ...
- 关于python文件操作 (转载)
总是记不住API.昨晚写的时候用到了这些,但是没记住,于是就索性整理一下吧: python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Pyth ...
- HDU 5481 Desiderium 动态规划
Desiderium Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=548 ...
- VMWare9下基于Ubuntu12.10搭建Hadoop-1.2.1集群
VMWare9下基于Ubuntu12.10搭建Hadoop-1.2.1集群 下一篇:VMWare9下基于Ubuntu12.10搭建Hadoop-1.2.1集群-整合Zookeeper和Hbase 近期 ...
- java复习1 java简单介绍
在学校的时候.学JAVA学的模棱两可,半知半解.工作以后给我带来了非常大的困扰,所以我须要在学一遍.如今就開始吧... . java[1]是一种能够撰写跨平台应用软件的面向对象的程序设计语言,是由Su ...
- java_Cookies_1_商品浏览历史记录servlet2
public class CookiesServlet2 extends HttpServlet { // 显示商品详细信息 public void doGet(HttpServletRequest ...
- Java Web services: WS-Security with Metro--referenc
As you know from "Introducing Metro," the reference implementations of the JAXB 2.x data-b ...
- Getting started: A skeleton application
Getting started: A skeleton application In order to build our application, we will start with theZen ...