暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
C. Amr and Chemistry
Problem's Link: http://codeforces.com/problemset/problem/558/C
Mean:
给出n个数,让你通过下面两种操作,把它们转换为同一个数。求最少的操作数。
1.ai = ai*2
2.ai = ai/2 (向下取整)
analyse:
基本思路:首先枚举出每个数能够到达的数字并且记录下到达该数组需要的步数,然后从到达次数为n次的数字中选择步数最小的即为答案。
对于一个数字Ai,它可以变换得到的数字可以分为三类:
1.向左移动n位得到的数字;
2.向右移动n位得到的数字;
3.奇数/2后得到的偶数向右移动n位得到的数字。
处理一个数 Ai:
1、对 Ai 执行左移操作,记录 Ai 通过左移能够得到的数字,上限为1e5,vis 数组加1,cnt数组记录步数
2、对 Ai 执行右移操作,直到 Ai 为0.
若 Ai 的最低位为1,右移一步之后,进行左移,上限为1e5,维持vis数组和cnt数组.
若 Ai 的最低位为0,右移一步,维持vis数组和cnt数组.
这样我们就把一个数的所有情况都处理出来了,并且是最少的操作数.
最后遍历数组找 vis[i] 为n,并且操作数最小。
Time complexity: O(n*m*log(MAX))
Source code:
/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-07-15-18.53
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL long long
#define ULL unsigned long long
using namespace std; const int MAXN(+int(1e5));
int arrive_step[MAXN<<];
int arrive_num[MAXN<<];
int a[MAXN],n; void init()
{
for(int i=; i<MAXN<<; ++i) { arrive_step[i]=,arrive_num[i]=; }
} inline void count_arrive(int &x)
{
int t=x,step=;
arrive_num[t]++;
while(t<=int(1e5))
{
t<<=;
step++;
arrive_step[t]+=step;
arrive_num[t]++;
}
t=x,step=;
while(t>)
{
if(t!= && (t&))
{
int sta=t>>;
int sstep=step+;
while(sta<=int(1e5))
{
sta<<=;
sstep++;
arrive_step[sta]+=sstep;
arrive_num[sta]++;
}
}
t>>=;
step++;
arrive_step[t]+=step;
arrive_num[t]++;
}
} int main()
{
ios_base::sync_with_stdio(false);
cin.tie();
while(~scanf("%d",&n))
{
init();
for(int i=; i<n; ++i)
{
scanf("%d",&a[i]);
count_arrive(a[i]);
}
int ans=INT_MAX;
for(int i=; i<(MAXN<<); ++i)
{
if(arrive_num[i]==n)
ans=ans<arrive_step[i]?ans:arrive_step[i];
}
printf("%d\n",ans);
}
return ;
}
/* */
暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry的更多相关文章
- 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 Amr and Chemistry
题意: n个数.每次能够选一个数 让其 *=2 或者 /=2 问至少操作多少次使得全部数相等. 思路: 对于每一个数,计算出这个数能够变成哪些数,以及变成那个数的最小步数,用两个数组保存 cnt[i] ...
- 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 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 ...
- 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 ...
- CF 558 C. Amr and Chemistry 暴力+二进制
链接:http://codeforces.com/problemset/problem/558/C C. Amr and Chemistry time limit per test 1 second ...
随机推荐
- quick -- 创建精灵和动作
local imgBg = display.newSprite("666666.jpg") :pos(display.cx, display.cy) :addTo(self) , ...
- 九宫格抽奖HTML+JS版
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 使用adb shell dumpsys检测Android的Activity任务栈
谈起Android程序开发,就需要了解其四个主要的部件:Activity.Service.ContentProvider. BroadcastReceiver.而其中Activity是唯一直接控制程序 ...
- Navi.Soft30.产品.Net对象查看器.操作手册
1系统简介 1.1功能简述 在软件开发过程中,我们会编写各种类以及创建类的属性,方法,事件等.特别是第三方控件或组件,刚拿到手时,若没有完善的开发文档,很难下手.这时,若是可以查看这些DLL的成员对象 ...
- ECshop 怎样修改商品详细页的“浏览次数”
怎样修改商品详细页的“浏览次数” 最好可以修改为成倍增加的,比如客户浏览了一次,显示的是20次. 修改 goods.php 文件的下面这行代码即可 $db->query('UPDATE ' . ...
- ECSHOP会员登录后直接进用户中心
ECSHOP系统在会员登录成功后,不是直接进入用户中心,而是跳转回了上一个页面或者是跳转到了首页. 注意:这里说的是,用户没有主动点击前往哪个页面,让系统自动跳转. 那如何让会员登录成功后自动进入“用 ...
- 将十六进制的颜色字符串转为UIColor
扩展UIColor,将十六进制的颜色字符串转成UIColor对象. extension UIColor { static func colorWithHexString(hex:String) -&g ...
- emoji和utf8mb4字符集
mysql 的 utf8 不支持 emoji,需要修改设置为utf8mb4 <?php 'mysql' => [ 'charset' => 'utf8mb4', 'collation ...
- Spring AOP Schema aop:config、tx:advice
Spring AOP Schema aop:config.tx:advice 一. 利用aop:config标签实现AOP 首先看个例子,如下 接口代码: package com.lei. ...
- Objective-C学习备忘录:Clang编译器编译运行Objective-C代码
我们都知道可以通过Apple公司的Xcode工具来学习Objective-C编程语言,但是能不能脱离XCode这个IDE进行Objective-C学习呢?当然是可以的.首先作为计算机科班出身的程序员都 ...